-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
useMap.tsx
136 lines (128 loc) · 3.09 KB
/
useMap.tsx
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
import { useState, useMemo, useEffect, useContext } from 'react';
import { useSetStatus, useEventProperties, useSettingProperties } from '@uiw/react-amap-utils';
import { MapProps } from '.';
import { Context } from './context';
export interface OverlayProps extends MapChildProps {}
/**
* 此类型是 `<Map>` 组件传递给子组件(如 `<Marker>`)的两个 props
*/
export interface MapChildProps {
/**
* 地图API的核心类,SDK加载完成才有
*/
AMap?: typeof AMap;
/**
* 实例化后的地图对象
*/
map?: AMap.Map;
}
export interface UseMap extends MapProps, MapChildProps {
/**
* 指定的容器
*/
container?: HTMLDivElement | null;
}
export const useMap = (props: UseMap = {}) => {
const { ...other } = props;
const [map, setMap] = useState<AMap.Map>();
const [zoom, setZoom] = useState(props.zoom || 15);
const [container, setContainer] = useState<HTMLDivElement | null | undefined>(props.container);
const { dispatch } = useContext(Context);
useMemo(() => {
if (container && !map && AMap) {
container.className = container.className + ' react-amap-wapper';
const instance = new AMap.Map(container, { zoom, ...other });
setMap(instance);
}
return () => {
if (map) {
map.destroy();
map.clearMap();
map.clearInfoWindow();
map.clearLimitBounds();
setMap(undefined);
}
};
}, [container, map]);
useEffect(() => {
if (map && container) {
dispatch({ map, container, AMap });
}
return () => {
dispatch({ map: undefined, container: undefined, AMap: undefined });
};
}, [map, container]);
useMemo(() => {
if (map && typeof props.zoom === 'number' && zoom !== props.zoom && props.zoom >= 2 && props.zoom <= 20) {
setZoom(props.zoom);
map.setZoom(props.zoom);
}
}, [zoom, props.zoom]);
useMemo(() => {
if (props.center && map) {
map.setCenter(props.center);
}
}, [map, props.center]);
useSetStatus<AMap.Map, UseMap>(map!, props, [
'dragEnable',
'zoomEnable',
'jogEnable',
'pitchEnable',
'rotateEnable',
'animateEnable',
'keyboardEnable',
]);
// setStatus, setZoomAndCenter, setFitView
// 'Center',
useSettingProperties<AMap.Map, UseMap>(map!, props, [
'Zoom',
'LabelzIndex',
'Layers',
'City',
'Bounds',
'LimitBounds',
'Lang',
'Rotation',
'DefaultCursor',
'MapStyle',
'Features',
'DefaultLayer',
'Pitch',
]);
useEventProperties<AMap.Map, UseMap>(map!, props, [
'onMouseMove',
'onZoomChange',
'onMapMove',
'onMouseWheel',
'onZoomStart',
'onMouseOver',
'onMouseOut',
'onDblClick',
'onClick',
'onZoomEnd',
'onMoveEnd',
'onMouseUp',
'onMouseDown',
'onRightClick',
'onMoveStart',
'onDragStart',
'onDragging',
'onDragEnd',
'onHotspotOut',
'onHotspotOver',
'onTouchStart',
'onComplete',
'onHotspotClick',
'onTouchMove',
'onTouchEnd',
'onResize',
]);
return {
map,
setMap,
zoom,
setZoom,
container,
setContainer,
};
};