All files / geolocation-control/src useGeolocationControl.tsx

50% Statements 8/16
0% Branches 0/11
33.33% Functions 1/3
50% Lines 8/16

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                1x 1x 1x 1x                                           1x 1x           1x 1x          
import { useEffect, useState } from 'react';
import { useMapContext } from '@uiw/react-baidu-map-map';
import { useProperties, useVisiable, useEventProperties } from '@uiw/react-baidu-map-utils';
import { GeolocationControlProps } from './';
 
export interface UseGeolocationControl extends GeolocationControlProps {}
 
export function useGeolocationControl(props = {} as UseGeolocationControl) {
  const [geolocationControl, setGeolocationControl] = useState<BMap.GeolocationControl>();
  const { anchor, offset, showAddressBar, enableAutoLocation, locationIcon } = props;
  const { map } = useMapContext();
  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]);
 
  useVisiable(geolocationControl!, props);
  useEventProperties<BMap.GeolocationControl, UseGeolocationControl>(
    geolocationControl!,
    props,
    ['LocationSuccess', 'LocationError'],
    'CamelCase',
  );
  useProperties<BMap.GeolocationControl, UseGeolocationControl>(geolocationControl!, props, ['Anchor', 'Offset']);
  return {
    geolocationControl,
    setGeolocationControl,
  };
}