All files / map/src useMap.tsx

34.69% Statements 17/49
12% Branches 6/50
22.22% Functions 2/9
34.69% Lines 17/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                          1x 1x 1x 1x 1x 1x 1x                                                           1x               1x                   1x       1x                       1x       1x                     1x                                                                       1x                         1x                   1x                          
import { useEffect, useState, useMemo, useContext } from 'react';
import { useEnableProperties, useProperties, useEventProperties } from '@uiw/react-baidu-map-utils';
import { MapProps } from './';
import { Context } from './context';
 
export interface OverlayProps extends MapChildProps {
  /**
   * 覆盖物是否可见
   */
  visiable?: boolean;
}
 
/**
 * 此类型是 `<Map>` 组件传递给子组件(如 `<Marker>`)的两个 `props`
 */
export interface MapChildProps {
  /**
   * 地图API的核心类,SDK加载完成才有
   */
  BMap?: typeof BMap;
  /**
   * 实例化后的地图对象
   */
  map?: BMap.Map;
}
 
export interface UseMap extends MapProps, MapChildProps {}
 
export function useMap(props: UseMap = {}) {
  const { widget, minZoom, maxZoom, mapType, enableHighResolution, enableAutoResize, enableMapClick } = props;
  const [map, setMap] = useState<BMap.Map>();
  const [zoom, setZoom] = useState(props.zoom || 15);
  const [container, setContainer] = useState<HTMLDivElement | null | undefined>(props.container);
  const { dispatch } = useContext(Context);
  useMemo(() => {
    Iif (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] as any;
            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] as any;
            Control && instance.addControl(new Control(options));
          }
        });
      setMap(instance);
    }
    return () => {
      if (map) {
        map.clearOverlays();
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [container, map]);
 
  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] = useState(props.center || '上海');
  /**
   * 根据参数设置中心点
   */
  useEffect(() => {
    if (map && center) {
      let cent = center;
      if (center && (center as BMap.Point).lng && (center as BMap.Point).lat) {
        cent = new BMap.Point((center as BMap.Point).lng, (center as BMap.Point).lat);
        map.centerAndZoom(cent!, zoom!);
      }
      map.centerAndZoom(center, zoom!);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [center, map]);
 
  const [autoLocalCity, setAutoLocalCity] = useState(props.autoLocalCity);
  /**
   * IP定位获取当前城市,进行自动定位
   */
  useEffect(() => {
    if (map && autoLocalCity) {
      const myCity = new BMap.LocalCity();
      myCity.get((result) => {
        setCenter(result.name as any);
        setZoom(result.level as any);
        setAutoLocalCity(false);
      });
    }
  }, [autoLocalCity, map]);
 
  useEventProperties<BMap.Map, UseMap>(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',
  useProperties<BMap.Map, UseMap>(map!, props, [
    'DefaultCursor',
    'DraggingCursor',
    'MinZoom',
    'MaxZoom',
    'MapStyle',
    'MapStyleV2',
    'Panorama',
    'CurrentCity',
    'MapType',
    'Viewport',
    'Zoom',
  ]);
  useEnableProperties<BMap.Map, UseMap>(map!, props, [
    'Dragging',
    'ScrollWheelZoom',
    'DoubleClickZoom',
    'Keyboard',
    'InertialDragging',
    'ContinuousZoom',
    'PinchToZoom',
    'AutoResize',
  ]);
  return {
    map,
    setMap,
    zoom,
    setZoom,
    container,
    setContainer,
    center,
    setCenter,
    autoLocalCity,
    setAutoLocalCity,
  };
}