All files / marker/src useMarker.tsx

35.29% Statements 12/34
15.38% Branches 2/13
20% Functions 1/5
38.7% Lines 12/31

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                  1x                                                                                                                                           1x 1x 1x   1x                                               1x       1x               1x 1x                             1x 1x                         1x              
import { useMapContext } from '@uiw/react-baidu-map-map';
import { useEffect, useState } from 'react';
// import defaultIconUrl from './markers.png';
import { noop, useEnableProperties, useEventProperties, useProperties, useVisiable } from '@uiw/react-baidu-map-utils';
import { MarkerProps } from '.';
import { defaultIconUrl } from './markers';
 
export interface UseMarker extends MarkerProps {}
 
const getIcons = (name: string) => {
  const icons = {
    simple_red: new BMap.Icon(defaultIconUrl, new BMap.Size(42 / 2, 66 / 2), {
      imageOffset: new BMap.Size(-454 / 2, -378 / 2),
      anchor: new BMap.Size(42 / 2 / 2, 66 / 2),
    }),
    simple_blue: new BMap.Icon(defaultIconUrl, new BMap.Size(42 / 2, 66 / 2), {
      imageOffset: new BMap.Size(-454 / 2, -450 / 2),
      anchor: new BMap.Size(42 / 2 / 2, 66 / 2),
    }),
    loc_red: new BMap.Icon(defaultIconUrl, new BMap.Size(46 / 2, 70 / 2), {
      imageOffset: new BMap.Size(-400 / 2, -378 / 2),
      anchor: new BMap.Size(46 / 2 / 2, 70 / 2),
    }),
    loc_blue: new BMap.Icon(defaultIconUrl, new BMap.Size(46 / 2, 70 / 2), {
      imageOffset: new BMap.Size(-400 / 2, -450 / 2),
      anchor: new BMap.Size(46 / 2 / 2, 70 / 2),
    }),
    dot_red: new BMap.Icon(defaultIconUrl, new BMap.Size(16 / 2, 16 / 2), {
      imageOffset: new BMap.Size(-216 / 2, -466 / 2),
      anchor: new BMap.Size(16 / 2 / 2, 16 / 2),
    }),
    dot_blue: new BMap.Icon(defaultIconUrl, new BMap.Size(16 / 2, 16 / 2), {
      imageOffset: new BMap.Size(-216 / 2, -486 / 2),
      anchor: new BMap.Size(16 / 2 / 2, 16 / 2),
    }),
    start: new BMap.Icon(defaultIconUrl, new BMap.Size(50 / 2, 80 / 2), {
      imageOffset: new BMap.Size(-400 / 2, -278 / 2),
      anchor: new BMap.Size(50 / 2 / 2, 80 / 2),
    }),
    end: new BMap.Icon(defaultIconUrl, new BMap.Size(50 / 2, 80 / 2), {
      imageOffset: new BMap.Size(-450 / 2, -278 / 2),
      anchor: new BMap.Size(50 / 2 / 2, 80 / 2),
    }),
    location: new BMap.Icon(defaultIconUrl, new BMap.Size(28 / 2, 40 / 2), {
      imageOffset: new BMap.Size(-248 / 2, -466 / 2),
      anchor: new BMap.Size(28 / 2 / 2, 40 / 2),
    }),
  };
 
  for (let i = 1; i <= 10; i++) {
    (icons as any)['red' + i] = new BMap.Icon(defaultIconUrl, new BMap.Size(42 / 2, 66 / 2), {
      imageOffset: new BMap.Size(0 - (42 / 2) * (i - 1), 0),
      anchor: new BMap.Size(42 / 2 / 2, 66 / 2),
    });
  }
 
  for (let i = 1; i <= 10; i++) {
    (icons as any)['blue' + i] = new BMap.Icon(defaultIconUrl, new BMap.Size(42 / 2, 66 / 2), {
      imageOffset: new BMap.Size(0 - (42 / 2) * (i - 1), -132 / 2),
      anchor: new BMap.Size(42 / 2 / 2, 66 / 2),
    });
  }
  return icons[name as keyof typeof icons];
};
 
export function useMarker(props = {} as UseMarker) {
  const {
    position,
    animation,
    offset,
    icon,
    enableMassClear,
    enableDragging,
    enableClicking,
    raiseOnDrag,
    draggingCursor,
    rotation,
    shadow,
    title,
  } = props;
  const { map } = useMapContext();
  const [marker, setMarker] = useState<BMap.Marker>();
 
  useEffect(() => {
    if (!BMap || !map) return noop;
    const options = {
      offset,
      icon,
      enableMassClear,
      enableDragging,
      enableClicking,
      raiseOnDrag,
      draggingCursor,
      rotation,
      shadow,
      title,
    };
    const point = new BMap.Point(position.lng, position.lat);
    const newMarker = new BMap.Marker(point, options);
    map.addOverlay(newMarker);
    newMarker.setAnimation(animation);
    setMarker(newMarker);
    return () => {
      map.removeOverlay(newMarker);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [map]);
  const [type, setType] = useState(props.type || 'loc_blue');
  /**
   * 设置标注点 `图标`
   */
  useEffect(() => {
    if (map && marker && !icon && type) {
      const newIcon = getIcons(type as string);
      newIcon.setImageSize(new BMap.Size(600 / 2, 600 / 2));
      marker.setIcon(newIcon);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [type, marker]);
  useVisiable(marker!, props);
  useEventProperties<BMap.Marker, UseMarker>(marker!, props, [
    'Click',
    'DblClick',
    'MouseDown',
    'MouseUp',
    'MouseOut',
    'MouseOver',
    'Remove',
    'InfowindowClose',
    'InfowindowOpen',
    'DragStart',
    'Dragging',
    'DragEnd',
    'RightClick',
  ]);
  useEnableProperties<BMap.Marker, UseMarker>(marker!, props, ['Dragging', 'MassClear', 'Clicking']);
  useProperties<BMap.Marker, UseMarker>(marker!, props, [
    'Icon',
    'Position',
    'Animation',
    'Offset',
    'Label',
    'Title',
    'Top',
    'ZIndex',
    'Rotation',
    'Shadow',
  ]);
 
  return {
    marker,
    setMarker,
    type,
    setType,
  };
}