UNPKG

@uiw/react-baidu-map-map

Version:
130 lines (128 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useMap = useMap; var _react = require("react"); var _reactBaiduMapUtils = require("@uiw/react-baidu-map-utils"); var _context = require("./context"); /** * 此类型是 `<Map>` 组件传递给子组件(如 `<Marker>`)的两个 `props` */ function useMap() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; const { widget, minZoom, maxZoom, mapType, enableHighResolution, enableAutoResize, enableMapClick } = props; const [map, setMap] = (0, _react.useState)(); const [zoom, setZoom] = (0, _react.useState)(props.zoom || 15); const [container, setContainer] = (0, _react.useState)(props.container); const { dispatch } = (0, _react.useContext)(_context.Context); (0, _react.useMemo)(() => { if (container && !map && BMap) { const instance = new BMap.Map(container, { minZoom, maxZoom, mapType, enableHighResolution, enableAutoResize, enableMapClick }); /** * 加载控件 */ widget && widget.forEach(item => { if (!BMap) { return; } if (typeof item === 'string') { const Control = BMap[item]; Control && instance.addControl(new Control()); } else if (typeof item === 'object' && item.control && typeof item.control === 'function') { instance.addControl(item.control(BMap, instance)); } else if (typeof item === 'object' && item.name) { const options = typeof item.options === 'function' ? item.options(BMap, instance) : item.options; const Control = BMap[item.name]; Control && instance.addControl(new Control(options)); } }); setMap(instance); } return () => { if (map) { map.clearOverlays(); } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [container, map]); (0, _react.useEffect)(() => { if (map && container) { dispatch({ map, container, BMap }); } return () => { dispatch({ map: undefined, container: undefined, BMap: undefined }); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [map, container]); const [center, setCenter] = (0, _react.useState)(props.center || '上海'); /** * 根据参数设置中心点 */ (0, _react.useEffect)(() => { if (map && center) { let cent = center; if (center && center.lng && center.lat) { cent = new BMap.Point(center.lng, center.lat); map.centerAndZoom(cent, zoom); } map.centerAndZoom(center, zoom); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [center, map]); const [autoLocalCity, setAutoLocalCity] = (0, _react.useState)(props.autoLocalCity); /** * IP定位获取当前城市,进行自动定位 */ (0, _react.useEffect)(() => { if (map && autoLocalCity) { const myCity = new BMap.LocalCity(); myCity.get(result => { setCenter(result.name); setZoom(result.level); setAutoLocalCity(false); }); } }, [autoLocalCity, map]); (0, _reactBaiduMapUtils.useEventProperties)(map, props, ['Click', 'DblClick', 'RightClick', 'RightdblClick', 'MapTypeChange', 'MouseMove', 'MouseOver', 'MouseOut', 'MoveStart', 'Moving', 'MoveEnd', 'ZoomStart', 'ZoomEnd', 'AddOverlay', 'AddControl', 'RemoveControl', 'RemoveOverlay', 'ClearOverlays', 'DragStart', 'Dragging', 'DragEnd', 'AddTileLayer', 'RemoveTileLayer', 'Load', 'ReSize', 'HotspotClick', 'HotspotOver', 'HotspotOut', 'TilesLoaded', 'TouchStart', 'TouchMove', 'TouchEnd', 'LongPress']); // 'Center', (0, _reactBaiduMapUtils.useProperties)(map, props, ['DefaultCursor', 'DraggingCursor', 'MinZoom', 'MaxZoom', 'MapStyle', 'MapStyleV2', 'Panorama', 'CurrentCity', 'MapType', 'Viewport', 'Zoom']); (0, _reactBaiduMapUtils.useEnableProperties)(map, props, ['Dragging', 'ScrollWheelZoom', 'DoubleClickZoom', 'Keyboard', 'InertialDragging', 'ContinuousZoom', 'PinchToZoom', 'AutoResize']); return { map, setMap, zoom, setZoom, container, setContainer, center, setCenter, autoLocalCity, setAutoLocalCity }; }