UNPKG

@uiw/react-baidu-map-geolocation-control

Version:

Baidu Map geolocation-control Components for React.

51 lines (50 loc) 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useGeolocationControl = useGeolocationControl; var _react = require("react"); var _reactBaiduMapMap = require("@uiw/react-baidu-map-map"); var _reactBaiduMapUtils = require("@uiw/react-baidu-map-utils"); function useGeolocationControl() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; const [geolocationControl, setGeolocationControl] = (0, _react.useState)(); const { anchor, offset, showAddressBar, enableAutoLocation, locationIcon } = props; const { map } = (0, _reactBaiduMapMap.useMapContext)(); (0, _react.useEffect)(() => { if (map && !geolocationControl) { const instance = new BMap.GeolocationControl({ anchor: anchor || BMAP_ANCHOR_TOP_LEFT, offset, showAddressBar, enableAutoLocation, locationIcon }); map.addControl(instance); setGeolocationControl(instance); } return () => { if (map && geolocationControl) { try { map.removeControl(geolocationControl); } catch (error) {} } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [map, geolocationControl]); (0, _reactBaiduMapUtils.useVisiable)(geolocationControl, props); (0, _reactBaiduMapUtils.useEventProperties)(geolocationControl, props, ['LocationSuccess', 'LocationError'], 'CamelCase'); (0, _reactBaiduMapUtils.useProperties)(geolocationControl, props, ['Anchor', 'Offset']); return { geolocationControl, setGeolocationControl }; }