UNPKG

@uiw/react-amap-geolocation

Version:

鹰眼控件,用于显示缩略地图,显示于地图右下角,可以随主图的视口变化而变化,也可以配置成固定位置实现类似于南海附图的效果。

55 lines 1.56 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; var _excluded = ["type", "onComplete", "onError"]; import { useState, useMemo, useEffect } from 'react'; import { useMapContext } from '@uiw/react-amap-map'; export var useGeolocation = function useGeolocation(props) { if (props === void 0) { props = {}; } var { type = 'position', onComplete, onError } = props, other = _objectWithoutPropertiesLoose(props, _excluded); var [geolocation, setGeolocation] = useState(); var { map } = useMapContext(); useEffect(() => { if (AMap && !geolocation) { var instance; AMap.plugin(['AMap.Geolocation'], () => { instance = new AMap.Geolocation(_extends({}, other)); setGeolocation(instance); }); return () => { if (instance) { setGeolocation(undefined); } }; } }, [AMap]); function callback(status, result) { if (status === 'complete' && onComplete) { onComplete(result); } else if (onError) { onError(result); } } useMemo(() => { if (!/^(position|cityInfo)$/.test(type)) return; var funName = type === 'position' ? 'getCurrentPosition' : 'getCityInfo'; if (geolocation && map) { geolocation[funName](callback); map.addControl(geolocation); } else if (geolocation) { geolocation[funName](callback); } }, [geolocation]); return { geolocation, setGeolocation }; };