gdmap-utils 1.2.4 → 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/README.md +467 -2
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/LayerManager.ts +73 -65
- package/src/MapUtils.ts +343 -329
- package/src/layers/baseMarkerLayer/index.ts +1 -1
- package/src/types/baseMarkerLayer.d.ts +0 -1
- package/docs/README.md +0 -5
package/src/MapUtils.ts
CHANGED
|
@@ -1,329 +1,343 @@
|
|
|
1
|
-
import MapSourceImport from './MapSourceImport';
|
|
2
|
-
import MapMixin from './gdMap/gdHelper';
|
|
3
|
-
import type { AMap as gdAMap, loaderOpts, MapOptions } from './types/amap.d';
|
|
4
|
-
import type {
|
|
5
|
-
MapUtilsOpts,
|
|
6
|
-
mapIns,
|
|
7
|
-
MarkerLayerBaseType,
|
|
8
|
-
MarkerClusterLayerType,
|
|
9
|
-
LayerOpts,
|
|
10
|
-
ClusterMarkerLayerOpts,
|
|
11
|
-
} from './types/index.d';
|
|
12
|
-
import {
|
|
13
|
-
BaseMarkerLayerIns,
|
|
14
|
-
BaseMarkerLayer,
|
|
15
|
-
ClusterMarkerLayerIns,
|
|
16
|
-
ClusterMarkerLayer,
|
|
17
|
-
} from './layers/index';
|
|
18
|
-
import type { SetOptional, Simplify } from 'type-fest';
|
|
19
|
-
import LayerManager from './LayerManager';
|
|
20
|
-
|
|
21
|
-
type MapUtilsConstructor = typeof MapUtils;
|
|
22
|
-
|
|
23
|
-
type mapUtilsIns = InstanceType<typeof MapUtils>;
|
|
24
|
-
|
|
25
|
-
export class MapUtils {
|
|
26
|
-
// 地图实例信息
|
|
27
|
-
map: mapIns;
|
|
28
|
-
|
|
29
|
-
options: SetOptional<MapUtilsOpts['MapUtilsCreateOpts'], 'mountSelector'>;
|
|
30
|
-
|
|
31
|
-
LayerManager: LayerManager = new LayerManager();
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
*@category 高德地图工具
|
|
35
|
-
*/
|
|
36
|
-
static createAMapMarker = MapMixin.createAMapMarker;
|
|
37
|
-
/**
|
|
38
|
-
*@category 高德地图工具
|
|
39
|
-
*/
|
|
40
|
-
static createIcon = MapMixin.createIcon;
|
|
41
|
-
/**
|
|
42
|
-
*@category 高德地图工具
|
|
43
|
-
*/
|
|
44
|
-
static Size = MapMixin.Size;
|
|
45
|
-
/**
|
|
46
|
-
*@category 高德地图工具
|
|
47
|
-
*/
|
|
48
|
-
static Pixel = MapMixin.Pixel;
|
|
49
|
-
/**
|
|
50
|
-
*@category 高德地图工具
|
|
51
|
-
*/
|
|
52
|
-
static LngLat = MapMixin.LngLat;
|
|
53
|
-
/**
|
|
54
|
-
*@category 高德地图工具
|
|
55
|
-
*/
|
|
56
|
-
static createAMapInfoWindow = MapMixin.createAMapInfoWindow;
|
|
57
|
-
/**
|
|
58
|
-
*@category 高德地图工具
|
|
59
|
-
*/
|
|
60
|
-
static createAMapPolyline = MapMixin.createAMapPolyline;
|
|
61
|
-
/**
|
|
62
|
-
*@category 高德地图工具
|
|
63
|
-
*/
|
|
64
|
-
static loadPlugins = MapMixin.loadPlugins;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @ignore
|
|
68
|
-
*/
|
|
69
|
-
constructor(
|
|
70
|
-
opts: MapUtilsOpts[keyof MapUtilsOpts],
|
|
71
|
-
AMap: Simplify<typeof gdAMap>
|
|
72
|
-
) {
|
|
73
|
-
if (!(typeof AMap === 'object' && 'Map' in AMap)) {
|
|
74
|
-
throw MapUtils.error('AMap is not exist');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if ('mapIns' in opts && opts.mapIns instanceof AMap.Map) {
|
|
78
|
-
const { mapIns, ...rest } = opts;
|
|
79
|
-
this.map = opts.mapIns;
|
|
80
|
-
this.options = rest;
|
|
81
|
-
} else {
|
|
82
|
-
if (!('mountSelector' in opts)) {
|
|
83
|
-
throw MapUtils.error('mountSelector is not exist');
|
|
84
|
-
} else {
|
|
85
|
-
this.options = opts;
|
|
86
|
-
|
|
87
|
-
const { mountSelector, ...rest } = opts;
|
|
88
|
-
|
|
89
|
-
this.map = this.initMap(mountSelector, rest); //地图初始化
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* @param {AMap.EventType} type 地图事件类型
|
|
96
|
-
* @param {(e: any) => void} fn 回调函数
|
|
97
|
-
* @param {*} [context] 事件上下文,缺省为当前实例
|
|
98
|
-
* @param {boolean} [once] 事件是否执行一次
|
|
99
|
-
* @memberof MapUtils
|
|
100
|
-
*/
|
|
101
|
-
bindMapClickEvent(
|
|
102
|
-
type: AMap.EventType,
|
|
103
|
-
fn: (e: any) => void,
|
|
104
|
-
context?: any,
|
|
105
|
-
once?: boolean
|
|
106
|
-
) {
|
|
107
|
-
this.map.on(type, fn, context, once);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* @ignore
|
|
112
|
-
*/
|
|
113
|
-
initMap(id: string, opts: MapOptions): mapIns {
|
|
114
|
-
//参数要作检验吗
|
|
115
|
-
return new window.AMap.Map(id, opts);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* @ignore
|
|
120
|
-
* @return {*}
|
|
121
|
-
*/
|
|
122
|
-
createBaseMarkerLayer<
|
|
123
|
-
U extends {},
|
|
124
|
-
T extends MarkerLayerBaseType = 'markerLayer',
|
|
125
|
-
>(opts: LayerOpts<U, T>) {
|
|
126
|
-
const layer = new BaseMarkerLayer<U, T>(opts, this);
|
|
127
|
-
// @ts-ignore
|
|
128
|
-
this.LayerManager.addLayer(layer);
|
|
129
|
-
return layer;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 高德地图中 `MarkerCluster, MassMarks`用于渲染海量点,它们调用方式也比较接近,将其聚合为一个统一的图层管理
|
|
134
|
-
* 目前, 聚合图层只包装`MarkerCluster`
|
|
135
|
-
* @param {ClusterMarkerLayerOpts<U, T>} opts - 图层配置选项
|
|
136
|
-
*/
|
|
137
|
-
createClusterMarkerLayer<
|
|
138
|
-
U extends {},
|
|
139
|
-
T extends MarkerClusterLayerType = 'markerClusterLayer',
|
|
140
|
-
>(opts: ClusterMarkerLayerOpts<U, T>) {
|
|
141
|
-
const layer = new ClusterMarkerLayer<U, T>(opts, this);
|
|
142
|
-
// @ts-ignore
|
|
143
|
-
this.LayerManager.addLayer(layer);
|
|
144
|
-
return layer;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* `removeLayer`接收图层实例将其从mapUtils的关联中移除
|
|
149
|
-
* @param {(BaseMarkerLayerIns | ClusterMarkerLayerIns)} layer
|
|
150
|
-
*/
|
|
151
|
-
removeLayer(layer: BaseMarkerLayerIns | ClusterMarkerLayerIns) {
|
|
152
|
-
const isLayerExist = this.LayerManager.hasLayer(layer);
|
|
153
|
-
|
|
154
|
-
if (isLayerExist) {
|
|
155
|
-
this.LayerManager.removeLayer(layer);
|
|
156
|
-
} else {
|
|
157
|
-
MapUtils.error(`${layer.layerName} is not exist`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 根据地图上添加的覆盖物分布情况,自动缩放地图到合适的视野级别
|
|
163
|
-
* @param {Array} overlays - 覆盖物数组,缺省为全部覆盖物
|
|
164
|
-
* @param {Boolean} immediately - 是否立即过渡
|
|
165
|
-
* @param {Array<Number>} avoid - 四周边距,上、下、左、右
|
|
166
|
-
* @param {number} maxZoom - 最大地图zoom级别
|
|
167
|
-
*/
|
|
168
|
-
setFitView(...opts: Parameters<mapIns['setFitView']>): void {
|
|
169
|
-
this.map.setFitView(...opts);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 地图弹窗关闭
|
|
174
|
-
* @memberof MapUtils
|
|
175
|
-
*/
|
|
176
|
-
clearInfoWindow() {
|
|
177
|
-
this.map.clearInfoWindow();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* @
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
*
|
|
231
|
-
*/
|
|
232
|
-
|
|
233
|
-
this.LayerManager.
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
*
|
|
238
|
-
*/
|
|
239
|
-
|
|
240
|
-
this.LayerManager.
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
*
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
1
|
+
import MapSourceImport from './MapSourceImport';
|
|
2
|
+
import MapMixin from './gdMap/gdHelper';
|
|
3
|
+
import type { AMap as gdAMap, loaderOpts, MapOptions } from './types/amap.d';
|
|
4
|
+
import type {
|
|
5
|
+
MapUtilsOpts,
|
|
6
|
+
mapIns,
|
|
7
|
+
MarkerLayerBaseType,
|
|
8
|
+
MarkerClusterLayerType,
|
|
9
|
+
LayerOpts,
|
|
10
|
+
ClusterMarkerLayerOpts,
|
|
11
|
+
} from './types/index.d';
|
|
12
|
+
import {
|
|
13
|
+
BaseMarkerLayerIns,
|
|
14
|
+
BaseMarkerLayer,
|
|
15
|
+
ClusterMarkerLayerIns,
|
|
16
|
+
ClusterMarkerLayer,
|
|
17
|
+
} from './layers/index';
|
|
18
|
+
import type { SetOptional, Simplify } from 'type-fest';
|
|
19
|
+
import LayerManager from './LayerManager';
|
|
20
|
+
|
|
21
|
+
type MapUtilsConstructor = typeof MapUtils;
|
|
22
|
+
|
|
23
|
+
type mapUtilsIns = InstanceType<typeof MapUtils>;
|
|
24
|
+
|
|
25
|
+
export class MapUtils {
|
|
26
|
+
// 地图实例信息
|
|
27
|
+
map: mapIns;
|
|
28
|
+
|
|
29
|
+
options: SetOptional<MapUtilsOpts['MapUtilsCreateOpts'], 'mountSelector'>;
|
|
30
|
+
|
|
31
|
+
LayerManager: LayerManager = new LayerManager();
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*@category 高德地图工具
|
|
35
|
+
*/
|
|
36
|
+
static createAMapMarker = MapMixin.createAMapMarker;
|
|
37
|
+
/**
|
|
38
|
+
*@category 高德地图工具
|
|
39
|
+
*/
|
|
40
|
+
static createIcon = MapMixin.createIcon;
|
|
41
|
+
/**
|
|
42
|
+
*@category 高德地图工具
|
|
43
|
+
*/
|
|
44
|
+
static Size = MapMixin.Size;
|
|
45
|
+
/**
|
|
46
|
+
*@category 高德地图工具
|
|
47
|
+
*/
|
|
48
|
+
static Pixel = MapMixin.Pixel;
|
|
49
|
+
/**
|
|
50
|
+
*@category 高德地图工具
|
|
51
|
+
*/
|
|
52
|
+
static LngLat = MapMixin.LngLat;
|
|
53
|
+
/**
|
|
54
|
+
*@category 高德地图工具
|
|
55
|
+
*/
|
|
56
|
+
static createAMapInfoWindow = MapMixin.createAMapInfoWindow;
|
|
57
|
+
/**
|
|
58
|
+
*@category 高德地图工具
|
|
59
|
+
*/
|
|
60
|
+
static createAMapPolyline = MapMixin.createAMapPolyline;
|
|
61
|
+
/**
|
|
62
|
+
*@category 高德地图工具
|
|
63
|
+
*/
|
|
64
|
+
static loadPlugins = MapMixin.loadPlugins;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @ignore
|
|
68
|
+
*/
|
|
69
|
+
constructor(
|
|
70
|
+
opts: MapUtilsOpts[keyof MapUtilsOpts],
|
|
71
|
+
AMap: Simplify<typeof gdAMap>
|
|
72
|
+
) {
|
|
73
|
+
if (!(typeof AMap === 'object' && 'Map' in AMap)) {
|
|
74
|
+
throw MapUtils.error('AMap is not exist');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if ('mapIns' in opts && opts.mapIns instanceof AMap.Map) {
|
|
78
|
+
const { mapIns, ...rest } = opts;
|
|
79
|
+
this.map = opts.mapIns;
|
|
80
|
+
this.options = rest;
|
|
81
|
+
} else {
|
|
82
|
+
if (!('mountSelector' in opts)) {
|
|
83
|
+
throw MapUtils.error('mountSelector is not exist');
|
|
84
|
+
} else {
|
|
85
|
+
this.options = opts;
|
|
86
|
+
|
|
87
|
+
const { mountSelector, ...rest } = opts;
|
|
88
|
+
|
|
89
|
+
this.map = this.initMap(mountSelector, rest); //地图初始化
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {AMap.EventType} type 地图事件类型
|
|
96
|
+
* @param {(e: any) => void} fn 回调函数
|
|
97
|
+
* @param {*} [context] 事件上下文,缺省为当前实例
|
|
98
|
+
* @param {boolean} [once] 事件是否执行一次
|
|
99
|
+
* @memberof MapUtils
|
|
100
|
+
*/
|
|
101
|
+
bindMapClickEvent(
|
|
102
|
+
type: AMap.EventType,
|
|
103
|
+
fn: (e: any) => void,
|
|
104
|
+
context?: any,
|
|
105
|
+
once?: boolean
|
|
106
|
+
) {
|
|
107
|
+
this.map.on(type, fn, context, once);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @ignore
|
|
112
|
+
*/
|
|
113
|
+
initMap(id: string, opts: MapOptions): mapIns {
|
|
114
|
+
//参数要作检验吗
|
|
115
|
+
return new window.AMap.Map(id, opts);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @ignore
|
|
120
|
+
* @return {*}
|
|
121
|
+
*/
|
|
122
|
+
createBaseMarkerLayer<
|
|
123
|
+
U extends {},
|
|
124
|
+
T extends MarkerLayerBaseType = 'markerLayer',
|
|
125
|
+
>(opts: LayerOpts<U, T>) {
|
|
126
|
+
const layer = new BaseMarkerLayer<U, T>(opts, this);
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
this.LayerManager.addLayer(layer);
|
|
129
|
+
return layer;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 高德地图中 `MarkerCluster, MassMarks`用于渲染海量点,它们调用方式也比较接近,将其聚合为一个统一的图层管理
|
|
134
|
+
* 目前, 聚合图层只包装`MarkerCluster`
|
|
135
|
+
* @param {ClusterMarkerLayerOpts<U, T>} opts - 图层配置选项
|
|
136
|
+
*/
|
|
137
|
+
createClusterMarkerLayer<
|
|
138
|
+
U extends {},
|
|
139
|
+
T extends MarkerClusterLayerType = 'markerClusterLayer',
|
|
140
|
+
>(opts: ClusterMarkerLayerOpts<U, T>) {
|
|
141
|
+
const layer = new ClusterMarkerLayer<U, T>(opts, this);
|
|
142
|
+
// @ts-ignore
|
|
143
|
+
this.LayerManager.addLayer(layer);
|
|
144
|
+
return layer;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* `removeLayer`接收图层实例将其从mapUtils的关联中移除
|
|
149
|
+
* @param {(BaseMarkerLayerIns | ClusterMarkerLayerIns)} layer
|
|
150
|
+
*/
|
|
151
|
+
removeLayer(layer: BaseMarkerLayerIns | ClusterMarkerLayerIns) {
|
|
152
|
+
const isLayerExist = this.LayerManager.hasLayer(layer);
|
|
153
|
+
|
|
154
|
+
if (isLayerExist) {
|
|
155
|
+
this.LayerManager.removeLayer(layer);
|
|
156
|
+
} else {
|
|
157
|
+
MapUtils.error(`${layer.layerName} is not exist`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 根据地图上添加的覆盖物分布情况,自动缩放地图到合适的视野级别
|
|
163
|
+
* @param {Array} overlays - 覆盖物数组,缺省为全部覆盖物
|
|
164
|
+
* @param {Boolean} immediately - 是否立即过渡
|
|
165
|
+
* @param {Array<Number>} avoid - 四周边距,上、下、左、右
|
|
166
|
+
* @param {number} maxZoom - 最大地图zoom级别
|
|
167
|
+
*/
|
|
168
|
+
setFitView(...opts: Parameters<mapIns['setFitView']>): void {
|
|
169
|
+
this.map.setFitView(...opts);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 地图弹窗关闭
|
|
174
|
+
* @memberof MapUtils
|
|
175
|
+
*/
|
|
176
|
+
clearInfoWindow() {
|
|
177
|
+
this.map.clearInfoWindow();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 销毁地图实例,释放资源
|
|
182
|
+
* - 清空所有图层
|
|
183
|
+
* - 销毁地图实例
|
|
184
|
+
* - 清理 LayerManager
|
|
185
|
+
* @memberof MapUtils
|
|
186
|
+
*/
|
|
187
|
+
destroyMap() {
|
|
188
|
+
// 清空所有图层
|
|
189
|
+
this.LayerManager.clear();
|
|
190
|
+
// 销毁地图实例
|
|
191
|
+
this.map.destroy();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 地图跳转参数
|
|
196
|
+
* @typedef {Object} GotoOpts
|
|
197
|
+
* @property {number} zoom - 目标缩放级别
|
|
198
|
+
* @property {AMap.LngLat|[number,number]} center - 目标中心点(高德坐标实例或 [lng,lat] 数组)
|
|
199
|
+
* @property {boolean} [immediately=false] - 是否立即跳转,默认平滑过渡
|
|
200
|
+
* @property {number} [duration] - 过渡动画时长(毫秒)
|
|
201
|
+
*/
|
|
202
|
+
/**
|
|
203
|
+
* 将地图视图切换到指定状态
|
|
204
|
+
* @param {GotoOpts} opts 跳转配置
|
|
205
|
+
*/
|
|
206
|
+
seZoomAndCenter(opts: {
|
|
207
|
+
zoom?: number;
|
|
208
|
+
center?: AMap.LngLat | [number, number];
|
|
209
|
+
immediately?: boolean;
|
|
210
|
+
duration?: number;
|
|
211
|
+
}): void {
|
|
212
|
+
const curZoom = this.map.getZoom();
|
|
213
|
+
const lngAndLat = this.map.getCenter();
|
|
214
|
+
const curCenter: [number, number] = [lngAndLat.lng, lngAndLat.lat];
|
|
215
|
+
let { center = curCenter, zoom = curZoom, immediately, duration } = opts;
|
|
216
|
+
|
|
217
|
+
this.map.setZoomAndCenter(zoom, center, immediately, duration);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 图层`layerName`显示, `layerName`为创建图层所传递的参数,用于标识图层名称。
|
|
222
|
+
* @param {string} layerName - 图层名称
|
|
223
|
+
*/
|
|
224
|
+
showLayer(layerName: string): void {
|
|
225
|
+
this.LayerManager.show(layerName);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 图层`layerName`隐藏, `layerName`为创建图层所传递的参数,用于标识图层名称。
|
|
230
|
+
* @param {string} layerName - 图层名称
|
|
231
|
+
*/
|
|
232
|
+
hideLayer(layerName: string): void {
|
|
233
|
+
this.LayerManager.hide(layerName);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 地图显示所有图层
|
|
238
|
+
*/
|
|
239
|
+
showAllLayers(): void {
|
|
240
|
+
this.LayerManager.showAll();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 地图隐藏所有图层
|
|
245
|
+
*/
|
|
246
|
+
hideAllLayers(): void {
|
|
247
|
+
this.LayerManager.hideAll();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 地图重新渲染所有图层
|
|
252
|
+
*/
|
|
253
|
+
reloadLayers(): void {
|
|
254
|
+
this.LayerManager.reload();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @ignore
|
|
259
|
+
* @param msg
|
|
260
|
+
*/
|
|
261
|
+
static error(msg: string) {
|
|
262
|
+
console.error(`[MapUtils Error]:${msg}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export type { mapUtilsIns, MapUtilsConstructor };
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
*
|
|
270
|
+
* `createMapUtils`功能如下:
|
|
271
|
+
* 1. 支持将高德地图实例包装成工具函数 (为其扩展功能),
|
|
272
|
+
* 2. 地图创建并为其扩展功能,
|
|
273
|
+
* 3. 高德地图依赖加载 ( 高德AMap函数加载 )
|
|
274
|
+
* @param {MapUtilsOpts[keyof MapUtilsOpts]} opts
|
|
275
|
+
* - 工具函数配置选项,支持两种模式:
|
|
276
|
+
* 1. 使用现有地图实例(MapUtilsUseExistingOpts)
|
|
277
|
+
* 2. 创建新地图实例(MapUtilsCreateOpts)
|
|
278
|
+
*
|
|
279
|
+
* [MapOptions文档地址](https://lbs.amap.com/api/javascript-api-v2/documentation#map)
|
|
280
|
+
* ```
|
|
281
|
+
* interface MapUtilsCreateOpts extends MapOptions {
|
|
282
|
+
mountSelector: string; //选择器
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
interface MapUtilsUseExistingOpts extends MapOptions {
|
|
286
|
+
mapIns: mapIns; //高德地图实例
|
|
287
|
+
}
|
|
288
|
+
* ```
|
|
289
|
+
* @param {loaderOpts} [loaderOPts]
|
|
290
|
+
* - 高德地图环境加载所传递的配置对象
|
|
291
|
+
* ```
|
|
292
|
+
* type loaderOpts = {
|
|
293
|
+
key: string;
|
|
294
|
+
version: string;
|
|
295
|
+
plugins?: string[] | undefined;
|
|
296
|
+
AMapUI?: {
|
|
297
|
+
version?: string | undefined;
|
|
298
|
+
plugins?: string[] | undefined;
|
|
299
|
+
} | undefined;
|
|
300
|
+
Loca?: {
|
|
301
|
+
version?: string | undefined;
|
|
302
|
+
} | undefined;
|
|
303
|
+
}
|
|
304
|
+
* ```
|
|
305
|
+
* @return {*}
|
|
306
|
+
*/
|
|
307
|
+
export async function createMapUtils(
|
|
308
|
+
opts: MapUtilsOpts[keyof MapUtilsOpts],
|
|
309
|
+
loaderOPts?: loaderOpts
|
|
310
|
+
) {
|
|
311
|
+
// window上存在属性已经加载
|
|
312
|
+
let AMap = window.AMap;
|
|
313
|
+
|
|
314
|
+
if (loaderOPts) {
|
|
315
|
+
AMap = await initMapSource(loaderOPts);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return new MapUtils(opts, AMap);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
*
|
|
323
|
+
* @param {loaderOpts} Opts
|
|
324
|
+
* - 高德地图环境加载所传递的配置对象
|
|
325
|
+
* ```
|
|
326
|
+
* type loaderOpts = {
|
|
327
|
+
key: string;
|
|
328
|
+
version: string;
|
|
329
|
+
plugins?: string[] | undefined;
|
|
330
|
+
AMapUI?: {
|
|
331
|
+
version?: string | undefined;
|
|
332
|
+
plugins?: string[] | undefined;
|
|
333
|
+
} | undefined;
|
|
334
|
+
Loca?: {
|
|
335
|
+
version?: string | undefined;
|
|
336
|
+
} | undefined;
|
|
337
|
+
}
|
|
338
|
+
* ```
|
|
339
|
+
* @return {*}
|
|
340
|
+
*/
|
|
341
|
+
export async function initMapSource(Opts: loaderOpts) {
|
|
342
|
+
return await MapSourceImport.loadScript(Opts);
|
|
343
|
+
}
|