gdmap-utils 1.2.3 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +1 -1
- package/.prettierrc.json +17 -17
- package/README.md +467 -2
- package/dist/index.js +127 -7
- package/dist/index.js.map +1 -1
- package/docs/classes/MapUtils.md +153 -0
- package/docs/functions/createMapUtils.md +18 -0
- package/docs/functions/initMapSource.md +14 -0
- package/docs/globals.md +11 -0
- package/examples/1_init.html +23 -23
- package/examples/2_mapInit.html +48 -48
- package/examples/3_MarkerLayer.html +565 -565
- package/examples/4_LabelMarkerLayer.html +574 -574
- package/examples/5_markerCluster.html +528 -0
- package/index.html +134 -134
- package/package.json +54 -51
- package/scripts/cleanDocs.js +220 -0
- package/scripts/mergeDocs.js +129 -0
- package/src/LayerManager.ts +17 -1
- package/src/MapSourceImport.ts +23 -23
- package/src/MapUtils.ts +198 -22
- package/src/gdMap/gdHelper.ts +113 -85
- package/src/index.ts +3 -1
- package/src/layers/baseMarkerLayer/LabelMarkerLayer.ts +240 -240
- package/src/layers/baseMarkerLayer/MarkerLayer.ts +208 -208
- package/src/layers/baseMarkerLayer/index.ts +369 -354
- package/src/layers/clusterMarkerLayer/MarkerClusterLayer.ts +53 -53
- package/src/layers/clusterMarkerLayer/index.ts +204 -177
- package/src/layers/index.ts +9 -9
- package/src/types/MapUtils.d.ts +53 -53
- package/src/types/amap.d.ts +11 -11
- package/src/types/{BaseMarkerLayer.d.ts → baseMarkerLayer.d.ts} +86 -87
- package/src/types/clusterMarkerLayer.d.ts +89 -88
- package/src/types/index.d.ts +14 -14
- package/tsconfig.json +26 -26
- package/typedoc.json +22 -0
- package/webpack.config.js +125 -126
- package/src/gdMap/gdHelper.js +0 -194
package/src/gdMap/gdHelper.ts
CHANGED
|
@@ -1,85 +1,113 @@
|
|
|
1
|
-
const gdHelperMixin = {
|
|
2
|
-
/**
|
|
3
|
-
* 创建高德地图标注
|
|
4
|
-
* @param {AMap.MarkerOptions} options 标注配置
|
|
5
|
-
* @returns {AMap.Marker} 标注对象
|
|
6
|
-
*/
|
|
7
|
-
createAMapMarker(options: AMap.MarkerOptions): AMap.Marker {
|
|
8
|
-
return new AMap.Marker({
|
|
9
|
-
...options,
|
|
10
|
-
});
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 创建一个图标
|
|
15
|
-
* @param {[number, number]} size - 图标尺寸,格式为 [width, height]
|
|
16
|
-
* @param {string} image - 图片的 URL 地址
|
|
17
|
-
* @param {[number, number]} imageSize - 图标所用图片的大小,格式为 [width, height]
|
|
18
|
-
* @param {[number, number]} imageOffset - 图标取图的偏移量,格式为 [x, y]
|
|
19
|
-
* @returns {AMap.Icon} 图标对象
|
|
20
|
-
*/
|
|
21
|
-
createIcon(opts: {
|
|
22
|
-
size: [number, number];
|
|
23
|
-
image: string;
|
|
24
|
-
imageSize: [number, number];
|
|
25
|
-
imageOffset: [number, number];
|
|
26
|
-
}): AMap.Icon {
|
|
27
|
-
return new AMap.Icon({
|
|
28
|
-
size: this.Size(...opts.size),
|
|
29
|
-
image: opts.image,
|
|
30
|
-
imageSize: this.Size(...opts.imageSize),
|
|
31
|
-
imageOffset: this.Pixel(...opts.imageOffset),
|
|
32
|
-
});
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 地物对象的像素尺寸
|
|
37
|
-
* @param {number} width 宽度
|
|
38
|
-
* @param {number} height 高度
|
|
39
|
-
* @returns {AMap.Size} 尺寸对象
|
|
40
|
-
*/
|
|
41
|
-
Size(width: number, height: number): AMap.Size {
|
|
42
|
-
return new AMap.Size(width, height);
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 像素坐标,确定地图上的一个像素点
|
|
47
|
-
* @param {number} x x坐标
|
|
48
|
-
* @param {number} y y坐标
|
|
49
|
-
* @returns {AMap.Pixel} 像素对象
|
|
50
|
-
*/
|
|
51
|
-
Pixel(...rest: [number, number]): AMap.Pixel {
|
|
52
|
-
return new AMap.Pixel(...rest);
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 经纬度坐标,用来描述地图上的一个点位置
|
|
57
|
-
* @param {number} lng 经度值
|
|
58
|
-
* @param {number} lat 纬度值
|
|
59
|
-
* @param {boolean} noWrap 是否不进行标准化处理
|
|
60
|
-
* @returns {AMap.LngLat} 经纬度对象
|
|
61
|
-
*/
|
|
62
|
-
LngLat(lng: number, lat: number, noWrap?: boolean): AMap.LngLat {
|
|
63
|
-
return new AMap.LngLat(lng, lat, noWrap);
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
/*
|
|
67
|
-
创建点位信息窗体
|
|
68
|
-
*/
|
|
69
|
-
createAMapInfoWindow(opts: ConstructorParameters<typeof AMap.InfoWindow>[0]) {
|
|
70
|
-
const defIsCustom = true;
|
|
71
|
-
const defCloseWhenClickMap = true;
|
|
72
|
-
const {
|
|
73
|
-
isCustom = defIsCustom,
|
|
74
|
-
closeWhenClickMap = defCloseWhenClickMap,
|
|
75
|
-
...rest
|
|
76
|
-
} = opts!;
|
|
77
|
-
return new AMap.InfoWindow({
|
|
78
|
-
isCustom,
|
|
79
|
-
closeWhenClickMap,
|
|
80
|
-
...rest,
|
|
81
|
-
});
|
|
82
|
-
},
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
1
|
+
const gdHelperMixin = {
|
|
2
|
+
/**
|
|
3
|
+
* 创建高德地图标注
|
|
4
|
+
* @param {AMap.MarkerOptions} options 标注配置
|
|
5
|
+
* @returns {AMap.Marker} 标注对象
|
|
6
|
+
*/
|
|
7
|
+
createAMapMarker(options: AMap.MarkerOptions): AMap.Marker {
|
|
8
|
+
return new AMap.Marker({
|
|
9
|
+
...options,
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 创建一个图标
|
|
15
|
+
* @param {[number, number]} size - 图标尺寸,格式为 [width, height]
|
|
16
|
+
* @param {string} image - 图片的 URL 地址
|
|
17
|
+
* @param {[number, number]} imageSize - 图标所用图片的大小,格式为 [width, height]
|
|
18
|
+
* @param {[number, number]} imageOffset - 图标取图的偏移量,格式为 [x, y]
|
|
19
|
+
* @returns {AMap.Icon} 图标对象
|
|
20
|
+
*/
|
|
21
|
+
createIcon(opts: {
|
|
22
|
+
size: [number, number];
|
|
23
|
+
image: string;
|
|
24
|
+
imageSize: [number, number];
|
|
25
|
+
imageOffset: [number, number];
|
|
26
|
+
}): AMap.Icon {
|
|
27
|
+
return new AMap.Icon({
|
|
28
|
+
size: this.Size(...opts.size),
|
|
29
|
+
image: opts.image,
|
|
30
|
+
imageSize: this.Size(...opts.imageSize),
|
|
31
|
+
imageOffset: this.Pixel(...opts.imageOffset),
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 地物对象的像素尺寸
|
|
37
|
+
* @param {number} width 宽度
|
|
38
|
+
* @param {number} height 高度
|
|
39
|
+
* @returns {AMap.Size} 尺寸对象
|
|
40
|
+
*/
|
|
41
|
+
Size(width: number, height: number): AMap.Size {
|
|
42
|
+
return new AMap.Size(width, height);
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 像素坐标,确定地图上的一个像素点
|
|
47
|
+
* @param {number} x x坐标
|
|
48
|
+
* @param {number} y y坐标
|
|
49
|
+
* @returns {AMap.Pixel} 像素对象
|
|
50
|
+
*/
|
|
51
|
+
Pixel(...rest: [number, number]): AMap.Pixel {
|
|
52
|
+
return new AMap.Pixel(...rest);
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 经纬度坐标,用来描述地图上的一个点位置
|
|
57
|
+
* @param {number} lng 经度值
|
|
58
|
+
* @param {number} lat 纬度值
|
|
59
|
+
* @param {boolean} noWrap 是否不进行标准化处理
|
|
60
|
+
* @returns {AMap.LngLat} 经纬度对象
|
|
61
|
+
*/
|
|
62
|
+
LngLat(lng: number, lat: number, noWrap?: boolean): AMap.LngLat {
|
|
63
|
+
return new AMap.LngLat(lng, lat, noWrap);
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
创建点位信息窗体
|
|
68
|
+
*/
|
|
69
|
+
createAMapInfoWindow(opts: ConstructorParameters<typeof AMap.InfoWindow>[0]) {
|
|
70
|
+
const defIsCustom = true;
|
|
71
|
+
const defCloseWhenClickMap = true;
|
|
72
|
+
const {
|
|
73
|
+
isCustom = defIsCustom,
|
|
74
|
+
closeWhenClickMap = defCloseWhenClickMap,
|
|
75
|
+
...rest
|
|
76
|
+
} = opts!;
|
|
77
|
+
return new AMap.InfoWindow({
|
|
78
|
+
isCustom,
|
|
79
|
+
closeWhenClickMap,
|
|
80
|
+
...rest,
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 创建高德地图折线
|
|
86
|
+
* @param {Object} options 折线配置
|
|
87
|
+
* @returns {AMap.Polyline} 折线对象
|
|
88
|
+
*/
|
|
89
|
+
createAMapPolyline(options: AMap.PolylineOptions) {
|
|
90
|
+
const defOpts = {
|
|
91
|
+
showDir: true,
|
|
92
|
+
lineJoin: 'round',
|
|
93
|
+
strokeColor: '#28F', //蓝色
|
|
94
|
+
strokeOpacity: 1, //线透明度
|
|
95
|
+
strokeWeight: 6,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const opts = Object.assign(options, defOpts);
|
|
99
|
+
|
|
100
|
+
return new AMap.Polyline(opts);
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
loadPlugins(plugins: string | string[]) {
|
|
104
|
+
return new Promise(resolve => {
|
|
105
|
+
AMap.plugin(plugins, function () {
|
|
106
|
+
//@ts-expect-error
|
|
107
|
+
resolve();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default gdHelperMixin;
|
package/src/index.ts
CHANGED
|
@@ -1,240 +1,240 @@
|
|
|
1
|
-
import type { OverlaysLayer, mapIns, OverlayData } from '@/types';
|
|
2
|
-
import type { MapUtilsConstructor } from '@/MapUtils';
|
|
3
|
-
import { MapUtils } from '@/MapUtils';
|
|
4
|
-
|
|
5
|
-
class LabelMarkerLayer {
|
|
6
|
-
rawLayer: AMap.LabelsLayer;
|
|
7
|
-
|
|
8
|
-
map: mapIns;
|
|
9
|
-
|
|
10
|
-
events = new Map<AMap.EventType, () => void>();
|
|
11
|
-
|
|
12
|
-
constructor(map: mapIns, opts?: AMap.LabelsLayerOptions) {
|
|
13
|
-
this.map = map;
|
|
14
|
-
|
|
15
|
-
this.rawLayer = new AMap.LabelsLayer({
|
|
16
|
-
collision: true,
|
|
17
|
-
opacity: 1,
|
|
18
|
-
zIndex: 120,
|
|
19
|
-
allowCollision: true,
|
|
20
|
-
...opts,
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
this.map.add(this.rawLayer);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 将覆盖物数据转换为标记配置
|
|
28
|
-
* @param item 覆盖物数据
|
|
29
|
-
* @param index 索引
|
|
30
|
-
* @param getIconUrl 获取图标URL的方法
|
|
31
|
-
* @param getOverlayOpts 动态获取覆盖物配置的方法
|
|
32
|
-
* @param MapUtils MapUtils构造函数
|
|
33
|
-
* @returns 标记配置
|
|
34
|
-
*/
|
|
35
|
-
static convertOverlayDataToOvlOpts<U extends {}>(
|
|
36
|
-
item: OverlayData<U>,
|
|
37
|
-
index: number,
|
|
38
|
-
getIconUrl: (item: OverlayData<U>) => string,
|
|
39
|
-
getOverlayOpts: (
|
|
40
|
-
item: OverlayData<U>,
|
|
41
|
-
index: number,
|
|
42
|
-
MapUtils: MapUtilsConstructor
|
|
43
|
-
) => any,
|
|
44
|
-
MapUtils: MapUtilsConstructor
|
|
45
|
-
): AMap.LabelMarkerOptions {
|
|
46
|
-
const {
|
|
47
|
-
overlayData: { lon, lat, extData, title },
|
|
48
|
-
} = item;
|
|
49
|
-
|
|
50
|
-
// 获取当前覆盖物项的动态配置
|
|
51
|
-
const itemOpts = getOverlayOpts(item, index, MapUtils);
|
|
52
|
-
|
|
53
|
-
const ovlOpts: AMap.LabelMarkerOptions = {
|
|
54
|
-
position: [lon, lat],
|
|
55
|
-
extData,
|
|
56
|
-
...itemOpts,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
if (!item.labelShowed) {
|
|
60
|
-
const textOpts = ovlOpts.text ?? {
|
|
61
|
-
content: title,
|
|
62
|
-
direction: 'top',
|
|
63
|
-
style: {
|
|
64
|
-
fontSize: 16,
|
|
65
|
-
strokeWidth: 5,
|
|
66
|
-
},
|
|
67
|
-
zooms: [5, 20],
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
textOpts.content = '';
|
|
71
|
-
|
|
72
|
-
ovlOpts.text = textOpts;
|
|
73
|
-
} else {
|
|
74
|
-
ovlOpts.text = ovlOpts.text ?? {
|
|
75
|
-
content: title,
|
|
76
|
-
direction: 'top',
|
|
77
|
-
style: {
|
|
78
|
-
fontSize: 16,
|
|
79
|
-
strokeWidth: 5,
|
|
80
|
-
},
|
|
81
|
-
zooms: [5, 20],
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const imageUrl = getIconUrl(item);
|
|
86
|
-
|
|
87
|
-
const icon = {
|
|
88
|
-
image: '',
|
|
89
|
-
size: [25, 34] as [number, number],
|
|
90
|
-
anchor: 'bottom-center',
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
ovlOpts.icon = ovlOpts.icon ?? icon;
|
|
94
|
-
|
|
95
|
-
ovlOpts.icon.image = imageUrl;
|
|
96
|
-
|
|
97
|
-
return ovlOpts as AMap.LabelMarkerOptions;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
bindEventOverlay(clickType: AMap.EventType, callback: () => void) {
|
|
101
|
-
if (typeof callback !== 'function') {
|
|
102
|
-
MapUtils.error('Please provide an event callback function');
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
this.on(clickType, callback);
|
|
107
|
-
|
|
108
|
-
this.events.set(clickType, callback);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
addOverlayBindEvent(marker: InstanceType<typeof AMap.LabelMarker>) {
|
|
112
|
-
this.events.forEach((callback, clickType) => {
|
|
113
|
-
marker.on(clickType, callback);
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
createOverlays(ovOptList: Array<AMap.LabelMarkerOptions>) {
|
|
118
|
-
const labelMarkers = ovOptList.map(item => {
|
|
119
|
-
const labelMarker = new AMap.LabelMarker(item); //包装吗?
|
|
120
|
-
this.addOverlayBindEvent(labelMarker);
|
|
121
|
-
return labelMarker;
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
//@ts-expect-error
|
|
125
|
-
this.rawLayer.add(labelMarkers);
|
|
126
|
-
|
|
127
|
-
return labelMarkers;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
add(markers: Array<AMap.LabelMarkerOptions>) {
|
|
131
|
-
this.createOverlays(markers);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
remove(markers: Array<AMap.LabelMarker>) {
|
|
135
|
-
//@ts-expect-error
|
|
136
|
-
this.rawLayer.remove(markers);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
hide() {
|
|
140
|
-
this.rawLayer.hide();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
show() {
|
|
144
|
-
this.rawLayer.show();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
getAllOverlay(): AMap.LabelMarker[] {
|
|
148
|
-
//@ts-expect-error
|
|
149
|
-
return this.rawLayer.getAllOverlays();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
destroy() {
|
|
153
|
-
// @ts-expect-error
|
|
154
|
-
this.rawLayer.clear();
|
|
155
|
-
|
|
156
|
-
this.map.remove(this.rawLayer);
|
|
157
|
-
//@ts-expect-error
|
|
158
|
-
this.rawLayer = null;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
reload() {
|
|
162
|
-
// @ts-expect-error
|
|
163
|
-
this.rawLayer.clear();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
clearAllOvl() {
|
|
167
|
-
// @ts-expect-error
|
|
168
|
-
this.rawLayer.clear();
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
overlayFitMap() {
|
|
172
|
-
const labelMarkers = this.getAllOverlay();
|
|
173
|
-
this.map.setFitView(labelMarkers);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
findLayerOverlay(markerId: string | number) {
|
|
177
|
-
if (!markerId) {
|
|
178
|
-
MapUtils.error('Please provide a markerId');
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const labelMarkers: InstanceType<typeof AMap.LabelMarker>[] =
|
|
183
|
-
this.getAllOverlay();
|
|
184
|
-
|
|
185
|
-
const labelMarker = labelMarkers.find(item => {
|
|
186
|
-
return item.getExtData().id === markerId;
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
return labelMarker;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
refreshOverlayIcon(
|
|
193
|
-
labelMarker: InstanceType<typeof AMap.LabelMarker>,
|
|
194
|
-
iconOpts: AMap.LabelMarkerIconOptions
|
|
195
|
-
) {
|
|
196
|
-
if (!labelMarker) {
|
|
197
|
-
MapUtils.error('labelMarker is not found');
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
labelMarker.setIcon(iconOpts);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
refreshOverlayLabel(
|
|
205
|
-
labelMarker: InstanceType<typeof AMap.LabelMarker>,
|
|
206
|
-
labelOpts?: AMap.LabelMarkerTextOptions
|
|
207
|
-
) {
|
|
208
|
-
if (!labelMarker) {
|
|
209
|
-
MapUtils.error('labelMarker is not found');
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (labelOpts) {
|
|
214
|
-
labelMarker.setText(labelOpts);
|
|
215
|
-
} else {
|
|
216
|
-
const currentLabel = labelMarker.getText();
|
|
217
|
-
labelMarker.setText({
|
|
218
|
-
...currentLabel,
|
|
219
|
-
content: '', //清空可以生效吗
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
*
|
|
226
|
-
* labelLayer不存在on方法
|
|
227
|
-
* @param {AMap.EventType} clickType 事件类型
|
|
228
|
-
* @param {() => void} callback 事件函数
|
|
229
|
-
* @memberof LabelMarkerLayer
|
|
230
|
-
*/
|
|
231
|
-
on(clickType: AMap.EventType, callback: () => void) {
|
|
232
|
-
const labelMarkers = this.getAllOverlay();
|
|
233
|
-
|
|
234
|
-
labelMarkers.forEach(labelMarker => {
|
|
235
|
-
labelMarker.on(clickType, callback);
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export default LabelMarkerLayer;
|
|
1
|
+
import type { OverlaysLayer, mapIns, OverlayData } from '@/types';
|
|
2
|
+
import type { MapUtilsConstructor } from '@/MapUtils';
|
|
3
|
+
import { MapUtils } from '@/MapUtils';
|
|
4
|
+
|
|
5
|
+
class LabelMarkerLayer {
|
|
6
|
+
rawLayer: AMap.LabelsLayer;
|
|
7
|
+
|
|
8
|
+
map: mapIns;
|
|
9
|
+
|
|
10
|
+
events = new Map<AMap.EventType, () => void>();
|
|
11
|
+
|
|
12
|
+
constructor(map: mapIns, opts?: AMap.LabelsLayerOptions) {
|
|
13
|
+
this.map = map;
|
|
14
|
+
|
|
15
|
+
this.rawLayer = new AMap.LabelsLayer({
|
|
16
|
+
collision: true,
|
|
17
|
+
opacity: 1,
|
|
18
|
+
zIndex: 120,
|
|
19
|
+
allowCollision: true,
|
|
20
|
+
...opts,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
this.map.add(this.rawLayer);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 将覆盖物数据转换为标记配置
|
|
28
|
+
* @param item 覆盖物数据
|
|
29
|
+
* @param index 索引
|
|
30
|
+
* @param getIconUrl 获取图标URL的方法
|
|
31
|
+
* @param getOverlayOpts 动态获取覆盖物配置的方法
|
|
32
|
+
* @param MapUtils MapUtils构造函数
|
|
33
|
+
* @returns 标记配置
|
|
34
|
+
*/
|
|
35
|
+
static convertOverlayDataToOvlOpts<U extends {}>(
|
|
36
|
+
item: OverlayData<U>,
|
|
37
|
+
index: number,
|
|
38
|
+
getIconUrl: (item: OverlayData<U>) => string,
|
|
39
|
+
getOverlayOpts: (
|
|
40
|
+
item: OverlayData<U>,
|
|
41
|
+
index: number,
|
|
42
|
+
MapUtils: MapUtilsConstructor
|
|
43
|
+
) => any,
|
|
44
|
+
MapUtils: MapUtilsConstructor
|
|
45
|
+
): AMap.LabelMarkerOptions {
|
|
46
|
+
const {
|
|
47
|
+
overlayData: { lon, lat, extData, title },
|
|
48
|
+
} = item;
|
|
49
|
+
|
|
50
|
+
// 获取当前覆盖物项的动态配置
|
|
51
|
+
const itemOpts = getOverlayOpts(item, index, MapUtils);
|
|
52
|
+
|
|
53
|
+
const ovlOpts: AMap.LabelMarkerOptions = {
|
|
54
|
+
position: [lon, lat],
|
|
55
|
+
extData,
|
|
56
|
+
...itemOpts,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if (!item.labelShowed) {
|
|
60
|
+
const textOpts = ovlOpts.text ?? {
|
|
61
|
+
content: title,
|
|
62
|
+
direction: 'top',
|
|
63
|
+
style: {
|
|
64
|
+
fontSize: 16,
|
|
65
|
+
strokeWidth: 5,
|
|
66
|
+
},
|
|
67
|
+
zooms: [5, 20],
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
textOpts.content = '';
|
|
71
|
+
|
|
72
|
+
ovlOpts.text = textOpts;
|
|
73
|
+
} else {
|
|
74
|
+
ovlOpts.text = ovlOpts.text ?? {
|
|
75
|
+
content: title,
|
|
76
|
+
direction: 'top',
|
|
77
|
+
style: {
|
|
78
|
+
fontSize: 16,
|
|
79
|
+
strokeWidth: 5,
|
|
80
|
+
},
|
|
81
|
+
zooms: [5, 20],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const imageUrl = getIconUrl(item);
|
|
86
|
+
|
|
87
|
+
const icon = {
|
|
88
|
+
image: '',
|
|
89
|
+
size: [25, 34] as [number, number],
|
|
90
|
+
anchor: 'bottom-center',
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
ovlOpts.icon = ovlOpts.icon ?? icon;
|
|
94
|
+
|
|
95
|
+
ovlOpts.icon.image = imageUrl;
|
|
96
|
+
|
|
97
|
+
return ovlOpts as AMap.LabelMarkerOptions;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
bindEventOverlay(clickType: AMap.EventType, callback: () => void) {
|
|
101
|
+
if (typeof callback !== 'function') {
|
|
102
|
+
MapUtils.error('Please provide an event callback function');
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.on(clickType, callback);
|
|
107
|
+
|
|
108
|
+
this.events.set(clickType, callback);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
addOverlayBindEvent(marker: InstanceType<typeof AMap.LabelMarker>) {
|
|
112
|
+
this.events.forEach((callback, clickType) => {
|
|
113
|
+
marker.on(clickType, callback);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
createOverlays(ovOptList: Array<AMap.LabelMarkerOptions>) {
|
|
118
|
+
const labelMarkers = ovOptList.map(item => {
|
|
119
|
+
const labelMarker = new AMap.LabelMarker(item); //包装吗?
|
|
120
|
+
this.addOverlayBindEvent(labelMarker);
|
|
121
|
+
return labelMarker;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
//@ts-expect-error
|
|
125
|
+
this.rawLayer.add(labelMarkers);
|
|
126
|
+
|
|
127
|
+
return labelMarkers;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
add(markers: Array<AMap.LabelMarkerOptions>) {
|
|
131
|
+
this.createOverlays(markers);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
remove(markers: Array<AMap.LabelMarker>) {
|
|
135
|
+
//@ts-expect-error
|
|
136
|
+
this.rawLayer.remove(markers);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
hide() {
|
|
140
|
+
this.rawLayer.hide();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
show() {
|
|
144
|
+
this.rawLayer.show();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
getAllOverlay(): AMap.LabelMarker[] {
|
|
148
|
+
//@ts-expect-error
|
|
149
|
+
return this.rawLayer.getAllOverlays();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
destroy() {
|
|
153
|
+
// @ts-expect-error
|
|
154
|
+
this.rawLayer.clear();
|
|
155
|
+
|
|
156
|
+
this.map.remove(this.rawLayer);
|
|
157
|
+
//@ts-expect-error
|
|
158
|
+
this.rawLayer = null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
reload() {
|
|
162
|
+
// @ts-expect-error
|
|
163
|
+
this.rawLayer.clear();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
clearAllOvl() {
|
|
167
|
+
// @ts-expect-error
|
|
168
|
+
this.rawLayer.clear();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
overlayFitMap() {
|
|
172
|
+
const labelMarkers = this.getAllOverlay();
|
|
173
|
+
this.map.setFitView(labelMarkers);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
findLayerOverlay(markerId: string | number) {
|
|
177
|
+
if (!markerId) {
|
|
178
|
+
MapUtils.error('Please provide a markerId');
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const labelMarkers: InstanceType<typeof AMap.LabelMarker>[] =
|
|
183
|
+
this.getAllOverlay();
|
|
184
|
+
|
|
185
|
+
const labelMarker = labelMarkers.find(item => {
|
|
186
|
+
return item.getExtData().id === markerId;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return labelMarker;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
refreshOverlayIcon(
|
|
193
|
+
labelMarker: InstanceType<typeof AMap.LabelMarker>,
|
|
194
|
+
iconOpts: AMap.LabelMarkerIconOptions
|
|
195
|
+
) {
|
|
196
|
+
if (!labelMarker) {
|
|
197
|
+
MapUtils.error('labelMarker is not found');
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
labelMarker.setIcon(iconOpts);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
refreshOverlayLabel(
|
|
205
|
+
labelMarker: InstanceType<typeof AMap.LabelMarker>,
|
|
206
|
+
labelOpts?: AMap.LabelMarkerTextOptions
|
|
207
|
+
) {
|
|
208
|
+
if (!labelMarker) {
|
|
209
|
+
MapUtils.error('labelMarker is not found');
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (labelOpts) {
|
|
214
|
+
labelMarker.setText(labelOpts);
|
|
215
|
+
} else {
|
|
216
|
+
const currentLabel = labelMarker.getText();
|
|
217
|
+
labelMarker.setText({
|
|
218
|
+
...currentLabel,
|
|
219
|
+
content: '', //清空可以生效吗
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
*
|
|
226
|
+
* labelLayer不存在on方法
|
|
227
|
+
* @param {AMap.EventType} clickType 事件类型
|
|
228
|
+
* @param {() => void} callback 事件函数
|
|
229
|
+
* @memberof LabelMarkerLayer
|
|
230
|
+
*/
|
|
231
|
+
on(clickType: AMap.EventType, callback: () => void) {
|
|
232
|
+
const labelMarkers = this.getAllOverlay();
|
|
233
|
+
|
|
234
|
+
labelMarkers.forEach(labelMarker => {
|
|
235
|
+
labelMarker.on(clickType, callback);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export default LabelMarkerLayer;
|