UNPKG

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

Version:

Baidu Map geolocation-control Components for React.

47 lines 1.36 kB
import { useEffect, useState } from 'react'; import { useMapContext } from '@uiw/react-baidu-map-map'; import { useProperties, useVisiable, useEventProperties } from '@uiw/react-baidu-map-utils'; export function useGeolocationControl(props) { if (props === void 0) { props = {}; } var [geolocationControl, setGeolocationControl] = useState(); var { anchor, offset, showAddressBar, enableAutoLocation, locationIcon } = props; var { map } = useMapContext(); useEffect(() => { if (map && !geolocationControl) { var 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]); useVisiable(geolocationControl, props); useEventProperties(geolocationControl, props, ['LocationSuccess', 'LocationError'], 'CamelCase'); useProperties(geolocationControl, props, ['Anchor', 'Offset']); return { geolocationControl, setGeolocationControl }; }