gdmap-utils 1.2.0 → 1.2.3
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 +1 -49
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
- package/src/LayerManager.ts +13 -5
- package/src/MapUtils.ts +15 -2
- package/src/layers/baseMarkerLayer/LabelMarkerLayer.ts +2 -0
- package/src/layers/baseMarkerLayer/MarkerLayer.ts +2 -0
- package/src/layers/clusterMarkerLayer/MarkerClusterLayer.ts +53 -0
- package/src/layers/clusterMarkerLayer/index.ts +177 -0
- package/src/layers/index.ts +4 -0
- package/src/types/clusterMarkerLayer.d.ts +88 -0
- package/src/types/index.d.ts +2 -0
- package/src/gdMapUtils.js +0 -377
- package/src/index.html +0 -14
- package/src/layers/MarkerClusterLayer.ts +0 -30
- package/src/layers copy/LabelMarkerLayer.js +0 -122
- package/src/layers copy/MarkerClusterLayer.js +0 -155
- package/src/layers copy/MarkerLayer.js +0 -267
- package/src/layers copy/OverlayGroupManager.js +0 -254
- package/src/layers copy/index.ts +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
mapIns,
|
|
3
|
+
MarkerClusterDataOptions,
|
|
4
|
+
MarkerClusterOptions,
|
|
5
|
+
} from '@/types/index.d';
|
|
6
|
+
|
|
7
|
+
class MarkerClusterLayer {
|
|
8
|
+
rawLayer: any; //高德未提供cluster的类型
|
|
9
|
+
|
|
10
|
+
map: mapIns;
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
map: mapIns,
|
|
14
|
+
dataOptions: Array<MarkerClusterDataOptions>,
|
|
15
|
+
opts: MarkerClusterOptions
|
|
16
|
+
) {
|
|
17
|
+
this.map = map;
|
|
18
|
+
//@ts-expect-error
|
|
19
|
+
this.rawLayer = new AMap.MarkerCluster(map, dataOptions, opts);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
bindEventMarker(clickType: AMap.EventType, callback: () => void) {
|
|
23
|
+
this.rawLayer.on(clickType, callback);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
add(dataOption: MarkerClusterDataOptions) {
|
|
27
|
+
this.rawLayer.addData(dataOption);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
remove(dataOptions: Array<MarkerClusterDataOptions>) {
|
|
31
|
+
this.rawLayer.setData(dataOptions);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
hide() {
|
|
35
|
+
this.rawLayer.setData([]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
show(dataOptions: Array<MarkerClusterDataOptions>) {
|
|
39
|
+
this.rawLayer.setData(dataOptions);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
destroy() {
|
|
43
|
+
this.rawLayer.setData([]);
|
|
44
|
+
this.rawLayer.setMap(null);
|
|
45
|
+
this.rawLayer = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
clearAllOvl() {
|
|
49
|
+
this.rawLayer.setData([]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default MarkerClusterLayer;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import MarkerClusterLayer from './MarkerClusterLayer';
|
|
2
|
+
import type {
|
|
3
|
+
MarkerClusterLayerType,
|
|
4
|
+
OverlayData, //OverLayerData图层公共接口
|
|
5
|
+
MarkerClusterDataOptions,
|
|
6
|
+
ClusterMarkerLayerInfo,
|
|
7
|
+
ClusterMarkerLayerOpts,
|
|
8
|
+
} from '@/types';
|
|
9
|
+
import { MapUtils } from '@/MapUtils';
|
|
10
|
+
import type { mapUtilsIns } from '@/MapUtils';
|
|
11
|
+
|
|
12
|
+
class ClusterMarkerLayer<
|
|
13
|
+
U extends object,
|
|
14
|
+
T extends MarkerClusterLayerType = 'markerClusterLayer',
|
|
15
|
+
V extends ClusterMarkerLayerInfo[T] = ClusterMarkerLayerInfo[T],
|
|
16
|
+
> {
|
|
17
|
+
// 图层类型与控制器类的映射关系
|
|
18
|
+
static layerClassMap = new Map<string, ClusterMarkerLayerTypeClass>();
|
|
19
|
+
/**
|
|
20
|
+
* 注册图层类型与控制器类的关联
|
|
21
|
+
* @param {string} layerType - 图层类型
|
|
22
|
+
* @param {Function} layerClass - 图层控制器类
|
|
23
|
+
*/
|
|
24
|
+
static registerLayer(
|
|
25
|
+
layerType: MarkerClusterLayerType,
|
|
26
|
+
layerClass: ClusterMarkerLayerTypeClass
|
|
27
|
+
) {
|
|
28
|
+
if (typeof layerType !== 'string' || typeof layerClass !== 'function') {
|
|
29
|
+
MapUtils.error('[LayerManager Error]: Invalid layer type or layer class');
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
ClusterMarkerLayer.layerClassMap.set(layerType, layerClass);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
overlayList: Array<OverlayData<U>>;
|
|
37
|
+
// 策略模式
|
|
38
|
+
rawLayerIns: V['layerIns'];
|
|
39
|
+
|
|
40
|
+
layerVisible: boolean = true;
|
|
41
|
+
|
|
42
|
+
layerName: string;
|
|
43
|
+
|
|
44
|
+
mapUtils: mapUtilsIns;
|
|
45
|
+
|
|
46
|
+
layerType: MarkerClusterLayerType;
|
|
47
|
+
|
|
48
|
+
constructor(opts: ClusterMarkerLayerOpts<U, T>, mapUtils: mapUtilsIns) {
|
|
49
|
+
const { layerType, layerName, ...rest } = opts;
|
|
50
|
+
|
|
51
|
+
const OverlaysLayer = ClusterMarkerLayer.layerClassMap.get(layerType);
|
|
52
|
+
|
|
53
|
+
if (OverlaysLayer) {
|
|
54
|
+
// 将OverlayData转换为MarkerClusterDataOptions
|
|
55
|
+
const clusterData = this.convertOverlayDataToClusterData(
|
|
56
|
+
opts.overlayList
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
this.rawLayerIns = new OverlaysLayer(
|
|
60
|
+
mapUtils.map,
|
|
61
|
+
clusterData,
|
|
62
|
+
opts.layerOpts
|
|
63
|
+
);
|
|
64
|
+
} else {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`[ClusterMarkerLayer Error]: Invalid layer type ${layerType}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.layerName = layerName;
|
|
71
|
+
|
|
72
|
+
this.mapUtils = mapUtils; //上层,mapUtils的实例
|
|
73
|
+
|
|
74
|
+
this.overlayList = opts.overlayList;
|
|
75
|
+
|
|
76
|
+
this.layerType = opts.layerType;
|
|
77
|
+
|
|
78
|
+
Object.assign(this, rest);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 将覆盖物数据转换为聚合标记所需的数据格式
|
|
83
|
+
* @param overlayList 覆盖物数据列表
|
|
84
|
+
* @returns 聚合标记数据列表
|
|
85
|
+
*/
|
|
86
|
+
private convertOverlayDataToClusterData(
|
|
87
|
+
overlayList: Array<OverlayData<U>>
|
|
88
|
+
): Array<MarkerClusterDataOptions> {
|
|
89
|
+
return overlayList.map(item => {
|
|
90
|
+
return {
|
|
91
|
+
lnglat: [item.overlayData.lon, item.overlayData.lat],
|
|
92
|
+
weight: item.overlayData.weight || 1,
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
bindEventOverlays(clickType: AMap.EventType, callback: () => void) {
|
|
98
|
+
this.rawLayerIns.bindEventMarker(clickType, callback);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
hide() {
|
|
102
|
+
this.layerVisible = false;
|
|
103
|
+
this.rawLayerIns.hide();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
show() {
|
|
107
|
+
this.layerVisible = true;
|
|
108
|
+
const clusterData = this.convertOverlayDataToClusterData(this.overlayList);
|
|
109
|
+
this.rawLayerIns.show(clusterData);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
destroy() {
|
|
113
|
+
// @ts-ignore
|
|
114
|
+
this.mapUtils.removeLayer(this); //从MapUtils中移除
|
|
115
|
+
this.rawLayerIns.destroy(); //地图层面移除
|
|
116
|
+
this.overlayList = [];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
clearAllOverlay() {
|
|
120
|
+
this.overlayList = [];
|
|
121
|
+
this.rawLayerIns.clearAllOvl();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
add(overlayList: Array<OverlayData<U>>) {
|
|
125
|
+
const clusterData = this.convertOverlayDataToClusterData(overlayList);
|
|
126
|
+
|
|
127
|
+
this.overlayList.push(...overlayList);
|
|
128
|
+
|
|
129
|
+
// 逐个添加聚合数据
|
|
130
|
+
clusterData.forEach(data => {
|
|
131
|
+
this.rawLayerIns.add(data);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
remove(ovs: Array<number | string>) {
|
|
136
|
+
if (Array.isArray(ovs)) {
|
|
137
|
+
// 根据ID移除覆盖物
|
|
138
|
+
const ids = ovs as Array<string | number>;
|
|
139
|
+
|
|
140
|
+
// 更新本地覆盖物列表
|
|
141
|
+
this.overlayList = this.overlayList.filter(
|
|
142
|
+
item => !ids.includes(item.id)
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// 重新设置聚合数据
|
|
146
|
+
const clusterData = this.convertOverlayDataToClusterData(
|
|
147
|
+
this.overlayList
|
|
148
|
+
);
|
|
149
|
+
this.rawLayerIns.remove(clusterData);
|
|
150
|
+
}
|
|
151
|
+
// 聚合图层不支持直接移除单个实例
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// 返回原始图层对象
|
|
155
|
+
getRawLayer() {
|
|
156
|
+
return this.rawLayerIns.rawLayer;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 注册聚合图层类型
|
|
161
|
+
ClusterMarkerLayer.registerLayer('markerClusterLayer', MarkerClusterLayer);
|
|
162
|
+
|
|
163
|
+
// MarkerClusterLayer 类型
|
|
164
|
+
export type MarkerClusterLayerIns = InstanceType<typeof MarkerClusterLayer>;
|
|
165
|
+
|
|
166
|
+
// 所有聚合图层类型的联合类型
|
|
167
|
+
export type ClusterMarkerLayerTypeIns = MarkerClusterLayerIns;
|
|
168
|
+
|
|
169
|
+
export type ClusterMarkerLayerTypeClass = typeof MarkerClusterLayer;
|
|
170
|
+
|
|
171
|
+
export type ClusterMarkerLayerClass = typeof ClusterMarkerLayer;
|
|
172
|
+
|
|
173
|
+
export type ClusterMarkerLayerIns = InstanceType<ClusterMarkerLayerClass>;
|
|
174
|
+
|
|
175
|
+
export { MarkerClusterLayer };
|
|
176
|
+
|
|
177
|
+
export default ClusterMarkerLayer;
|
package/src/layers/index.ts
CHANGED
|
@@ -2,4 +2,8 @@
|
|
|
2
2
|
// 从baseMarkerLayer目录导入BaseMarkerLayer相关内容并重新导出
|
|
3
3
|
export type * from './baseMarkerLayer';
|
|
4
4
|
|
|
5
|
+
export type * from './clusterMarkerLayer';
|
|
6
|
+
|
|
5
7
|
export { default as BaseMarkerLayer } from './baseMarkerLayer';
|
|
8
|
+
|
|
9
|
+
export { default as ClusterMarkerLayer } from './clusterMarkerLayer';
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { MarkerClusterLayerIns } from '@/layers/clusterMarkerLayer/index.ts';
|
|
2
|
+
import type { OverlayData } from './BaseMarkerLayer';
|
|
3
|
+
//定义聚合图层类型
|
|
4
|
+
type MarkerClusterLayerType = 'markerClusterLayer';
|
|
5
|
+
|
|
6
|
+
//!------------markerClusterLayer.ts类型代码如下------------------
|
|
7
|
+
// 图层配置信息
|
|
8
|
+
interface MarkerClusterOptions {
|
|
9
|
+
/** 聚合计算时网格的像素大小,默认 60 */
|
|
10
|
+
gridSize?: number;
|
|
11
|
+
/** 最大聚合级别,大于该级别不再聚合,默认 18 */
|
|
12
|
+
maxZoom?: number;
|
|
13
|
+
/**
|
|
14
|
+
* 聚合图标位置是否取所有点(或权重最大点)的中心。
|
|
15
|
+
* 默认为 true。
|
|
16
|
+
*/
|
|
17
|
+
averageCenter?: boolean;
|
|
18
|
+
/** 地图缩放过程中是否实时重算聚合,默认 false */
|
|
19
|
+
clusterByZoomChange?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 聚合图标样式数组,按聚合数量区间 1-10、11-100、101-1000… 顺序给出。
|
|
22
|
+
* 当数量超出给定样式时,剩余区间使用默认样式。
|
|
23
|
+
*/
|
|
24
|
+
styles?: Array<{
|
|
25
|
+
/** 图标图片地址(必选) */
|
|
26
|
+
url: string;
|
|
27
|
+
/** 图标尺寸(必选) */
|
|
28
|
+
size: AMap.Size;
|
|
29
|
+
/** 图标定位偏移,默认 (0,0) */
|
|
30
|
+
offset?: AMap.Pixel;
|
|
31
|
+
/** 图片背景偏移,等同 CSS background-position,默认 (0,0) */
|
|
32
|
+
imageOffset?: AMap.Pixel;
|
|
33
|
+
/** 文字颜色,默认 "#000000" */
|
|
34
|
+
textColor?: string;
|
|
35
|
+
/** 文字大小,默认 10 */
|
|
36
|
+
textSize?: number;
|
|
37
|
+
}>;
|
|
38
|
+
/**
|
|
39
|
+
* 自定义聚合点绘制逻辑,指定后 styles 失效。
|
|
40
|
+
* @param context 聚合上下文
|
|
41
|
+
*/
|
|
42
|
+
renderClusterMarker?: (context: {
|
|
43
|
+
/** 当前聚合包含的 Marker 数量 */
|
|
44
|
+
count: number;
|
|
45
|
+
/** 当前聚合所对应的 Marker 对象(可进一步修改其 Icon、内容等) */
|
|
46
|
+
marker: AMap.Marker;
|
|
47
|
+
}) => void;
|
|
48
|
+
/**
|
|
49
|
+
* 自定义非聚合点绘制逻辑。
|
|
50
|
+
* @param context 非聚合点上下文
|
|
51
|
+
*/
|
|
52
|
+
renderMarker?: (context: {
|
|
53
|
+
/** 非聚合的 Marker 对象 */
|
|
54
|
+
marker: AMap.Marker;
|
|
55
|
+
}) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type MarkerClusterDataOptions<T extends {} = {}> = {
|
|
59
|
+
weight: number;
|
|
60
|
+
lnglat: [number, number];
|
|
61
|
+
} & T;
|
|
62
|
+
|
|
63
|
+
type ClusterMarkerLayerInfo = {
|
|
64
|
+
markerClusterLayer: {
|
|
65
|
+
layerIns: MarkerClusterLayerIns;
|
|
66
|
+
layerOpts: MarkerClusterOptions;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
interface ClusterMarkerLayerOpts<
|
|
71
|
+
U = {},
|
|
72
|
+
T extends MarkerClusterLayerType = 'markerClusterLayer',
|
|
73
|
+
V = ClusterMarkerLayerInfo[T],
|
|
74
|
+
> {
|
|
75
|
+
layerType: T;
|
|
76
|
+
layerName: string;
|
|
77
|
+
overlayList: Array<OverlayData<U>>;
|
|
78
|
+
layerOpts: V['layerOpts'];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type {
|
|
82
|
+
MarkerClusterLayerType,
|
|
83
|
+
MarkerClusterOptions,
|
|
84
|
+
MarkerClusterDataOptions,
|
|
85
|
+
// 图层定义类型代码
|
|
86
|
+
ClusterMarkerLayerInfo,
|
|
87
|
+
ClusterMarkerLayerOpts,
|
|
88
|
+
};
|
package/src/types/index.d.ts
CHANGED
package/src/gdMapUtils.js
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
import AMapLoader from '@amap/amap-jsapi-loader';
|
|
2
|
-
import gdHelperMixin from './gdHelper.js'; //抽取的高德mixin工具函数
|
|
3
|
-
import OverlayGroupManager from './layers/OverlayGroupManager.js';
|
|
4
|
-
import eventMixin from '../eventMixin.js';
|
|
5
|
-
import LayerManager from './LayerManager.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 针对高德的二次封装
|
|
9
|
-
* 目前高德地图使用的是 GCJ-02 坐标,如果你采集的是 WGS84 坐标或者其他,请先进行坐标转换
|
|
10
|
-
*/
|
|
11
|
-
class GdMapUtils {
|
|
12
|
-
// 地图实例对象
|
|
13
|
-
map = null;
|
|
14
|
-
//高德AMap对象
|
|
15
|
-
AMap = null;
|
|
16
|
-
// 地图Ui对象
|
|
17
|
-
AMapUI = null;
|
|
18
|
-
// loadOpts加载的配置信息 地图配置和加载地图配置分开
|
|
19
|
-
loadOpts = {};
|
|
20
|
-
// 地图容器id
|
|
21
|
-
id = '';
|
|
22
|
-
// 地图的配置对象
|
|
23
|
-
mapOpts = {};
|
|
24
|
-
|
|
25
|
-
mapTitleLayers = {}; //图层map对象 第三方图层
|
|
26
|
-
|
|
27
|
-
// 缓存实例集合
|
|
28
|
-
static mapInstance = new Map();
|
|
29
|
-
|
|
30
|
-
overlayGroupManagerMap = new Map(); //HACK 是否移入到OverlayGroupManager中。
|
|
31
|
-
|
|
32
|
-
// 图层实例映射
|
|
33
|
-
layerInstances = new Map();
|
|
34
|
-
|
|
35
|
-
// 图层管理器
|
|
36
|
-
layerManager = LayerManager;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 加载地图和初始化地图分开
|
|
40
|
-
* @param {Object} options 加载高德初始化地图配置
|
|
41
|
-
*/
|
|
42
|
-
//TODO 构造函数能够接收this.map对象
|
|
43
|
-
constructor(options) {
|
|
44
|
-
if (!options) {
|
|
45
|
-
this.error('请传入配置对象');
|
|
46
|
-
}
|
|
47
|
-
// 某些API加载前必须设置秘钥
|
|
48
|
-
window._AMapSecurityConfig = {
|
|
49
|
-
securityJsCode: process.env.VUE_APP_GD_MAP_CODE, // 安全密钥
|
|
50
|
-
};
|
|
51
|
-
options.key = process.env.VUE_APP_GD_MAP_KEY;
|
|
52
|
-
this.loadOpts = options;
|
|
53
|
-
this.clickMapRestMarkers = options.clickMapRestMarkers ?? true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
error(msg) {
|
|
57
|
-
console.error(`[AmapUtils Error]:${msg}`);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 异步加载地图插件
|
|
62
|
-
* @param {String} plugins AMap.ToolBar
|
|
63
|
-
* @returns {Promise}
|
|
64
|
-
* @memberof GdMapUtils
|
|
65
|
-
*/
|
|
66
|
-
loadPlugins(plugins) {
|
|
67
|
-
return new Promise((resolve, reject) => {
|
|
68
|
-
this.AMap.plugin(plugins, function (result) {
|
|
69
|
-
resolve(result);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 异步加载UI插件
|
|
76
|
-
* @param {String} plugins overlay/AwesomeMarker
|
|
77
|
-
* @return {Promise}
|
|
78
|
-
* @memberof GdMapUtils
|
|
79
|
-
*/
|
|
80
|
-
loadUIPlugins(plugins) {
|
|
81
|
-
return new Promise((resolve, reject) => {
|
|
82
|
-
if (!this.AMapUI) {
|
|
83
|
-
reject(new Error('AMapUI is not initialized.')); // 提供错误信息
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
this.AMapUI.loadUI(plugins, function (result) {
|
|
87
|
-
if (result) {
|
|
88
|
-
resolve(result);
|
|
89
|
-
} else {
|
|
90
|
-
reject(new Error('Failed to load UI plugin.')); // 处理加载失败的情况
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 初始化地图
|
|
98
|
-
* @param {String} id DOM 的id
|
|
99
|
-
* @param {Object} options Map地图配置项
|
|
100
|
-
* @return {Promise}
|
|
101
|
-
* @memberof GdMapUtils
|
|
102
|
-
*/
|
|
103
|
-
initMap(id, options) {
|
|
104
|
-
this.id = id;
|
|
105
|
-
this.mapOpts = options;
|
|
106
|
-
return new Promise((resolve, reject) => {
|
|
107
|
-
// 确保每次的AMap都是合法的所以不分开创建
|
|
108
|
-
AMapLoader.load(this.loadOpts)
|
|
109
|
-
.then(AMap => {
|
|
110
|
-
// 将 AMap 全局对象挂载到 window 上
|
|
111
|
-
window.AMap = AMap;
|
|
112
|
-
|
|
113
|
-
this.AMapUI = window.AMapUI;
|
|
114
|
-
|
|
115
|
-
this.AMap = AMap;
|
|
116
|
-
|
|
117
|
-
this.map = new AMap.Map(this.id, this.mapOpts); //"container"为 <div> 容器的 id
|
|
118
|
-
|
|
119
|
-
resolve(this.map);
|
|
120
|
-
// 将当前实例存储到 mapInstance 中
|
|
121
|
-
GdMapUtils.mapInstance.set(id, this);
|
|
122
|
-
|
|
123
|
-
this.bindMapClickEvent(); //初始化绑定事件
|
|
124
|
-
})
|
|
125
|
-
.catch(e => {
|
|
126
|
-
reject(e);
|
|
127
|
-
console.error(e);
|
|
128
|
-
throw new Error(e);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* 连接到已有地图
|
|
134
|
-
* @param {String} id DOM 的id
|
|
135
|
-
* @param {Object} map 已经存在的地图实例
|
|
136
|
-
* @return {Promise}
|
|
137
|
-
* @memberof GdMapUtils
|
|
138
|
-
*/
|
|
139
|
-
linkToExistMap(id, gdMapIns) {
|
|
140
|
-
if (!id || !('options' in gdMapIns)) {
|
|
141
|
-
//HACK map上挂属性标识他为地图实例
|
|
142
|
-
return this.error('请传入正确的地图id和地图实例');
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this.id = id;
|
|
146
|
-
// 关联地图配置
|
|
147
|
-
this.mapOpts = gdMapIns.options;
|
|
148
|
-
|
|
149
|
-
this.AMap = AMap; //HACK 异步获取AMap对象, 根据map获取当前地图AMap
|
|
150
|
-
|
|
151
|
-
this.AMapUI = window.AMapUI; //HACK AMapUI 是个啥
|
|
152
|
-
|
|
153
|
-
this.map = gdMapIns; // 关联地图实例
|
|
154
|
-
|
|
155
|
-
this.bindMapClickEvent(); //初始化绑定事件
|
|
156
|
-
// 将当前实例存储到 mapInstance 中
|
|
157
|
-
GdMapUtils.mapInstance.set(id, this);
|
|
158
|
-
|
|
159
|
-
return this.map;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// 初始化绑定地图事件
|
|
163
|
-
bindMapClickEvent() {
|
|
164
|
-
this.map.on('click', () => {
|
|
165
|
-
if (this.clickMapRestMarkers) {
|
|
166
|
-
this.overlayGroupManagerMap.forEach(overlayGroup => {
|
|
167
|
-
overlayGroup.resetActiveMarker(); // 清除图层上的所有覆盖物
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
// 绑定缩放时间获取当前层级
|
|
173
|
-
this.map.on('zoomchange', () => {
|
|
174
|
-
const zoom = this.map.getZoom(); // 获取当前缩放级别
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
//给所有的marker绑定事件
|
|
179
|
-
bindEventMarker(type, clickType, callback) {
|
|
180
|
-
if (!this.getOverlayGroupManager(type)) {
|
|
181
|
-
return this.error('图层不存在,请检查输入!');
|
|
182
|
-
}
|
|
183
|
-
this.getOverlayGroupManager(type).bindEventMarker(clickType, callback); // 绑定事件到图层管理器
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
//创建点位
|
|
187
|
-
createMarker(type, Opts) {
|
|
188
|
-
const overlayGroupManager = this.createOverlayGroupManager(Opts, type); // 关联图层管理器
|
|
189
|
-
// 创建图标
|
|
190
|
-
const marker = new AMap.Marker(Opts);
|
|
191
|
-
|
|
192
|
-
// marker上地图
|
|
193
|
-
overlayGroupManager.addOverlay(marker);
|
|
194
|
-
|
|
195
|
-
return marker;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 关联图层管理器
|
|
199
|
-
createOverlayGroupManager(overlays, overlayType) {
|
|
200
|
-
const overlayManager = this.getOverlayGroupManager(overlayType); //获取图层管理器
|
|
201
|
-
|
|
202
|
-
if (overlayManager) return overlayManager; //图层已经关联了
|
|
203
|
-
|
|
204
|
-
const overlayGroupManager = new OverlayGroupManager({
|
|
205
|
-
overlays,
|
|
206
|
-
overlayType,
|
|
207
|
-
map: this.map,
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
this.overlayGroupManagerMap.set(overlayType, overlayGroupManager); //保存图层管理器
|
|
211
|
-
|
|
212
|
-
return overlayGroupManager;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// 获取图层管理器
|
|
216
|
-
getOverlayGroupManager(overlayType) {
|
|
217
|
-
if (typeof overlayType !== 'string' && overlayType.length === 0) {
|
|
218
|
-
return this.error('请传入图层类型');
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return this.overlayGroupManagerMap.get(overlayType);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
updateMarker(marker, Opts) {
|
|
225
|
-
if (!marker) {
|
|
226
|
-
return this.error('参数错误');
|
|
227
|
-
}
|
|
228
|
-
// 获取旧marker的类型
|
|
229
|
-
const { type } = marker.getExtData();
|
|
230
|
-
// 移除点位数据
|
|
231
|
-
this.removeMarker(type, marker);
|
|
232
|
-
// 更新Marker
|
|
233
|
-
this.createMarker(type, Opts);
|
|
234
|
-
}
|
|
235
|
-
//移除某一个marker或者多个marker
|
|
236
|
-
removeMarker(overlayType, overlay) {
|
|
237
|
-
if (!this.overlayGroupManagerMap.has(overlayType)) {
|
|
238
|
-
return this.error('图层不存在,请检查输入!');
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const overlayGroupManager = this.getOverlayGroupManager(overlayType);
|
|
242
|
-
|
|
243
|
-
overlayGroupManager.removeOverlay(overlay); // 关联图层管理器
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// 清楚所有覆盖物
|
|
247
|
-
removeAllOverlay() {
|
|
248
|
-
//BUG 这里只能移除用overlayGroupManagerManager管理的图层
|
|
249
|
-
this.overlayGroupManagerMap.forEach(overlayGroup => {
|
|
250
|
-
overlayGroup.OverlayGroup.clearOverlays(); // 清除图层上的所有覆盖物
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// 高德地图添加覆盖物
|
|
255
|
-
mapToAdd(overlay, autoFit = true) {
|
|
256
|
-
this.map.add(overlay);
|
|
257
|
-
// 调整到合适的视角
|
|
258
|
-
// autoFit && this.map.setFitView();
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// 创建海量点图层
|
|
262
|
-
createMarkerCluster(data, options) {
|
|
263
|
-
return new AMap.MarkerCluster(this.map, data, options);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// 创建标签图层
|
|
267
|
-
createLabelLayer(options) {
|
|
268
|
-
return new AMap.LabelLayer(options);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* 通过图层类型创建图层
|
|
273
|
-
* @param {string} layerType - 图层类型
|
|
274
|
-
* @param {Object} options - 图层配置选项
|
|
275
|
-
* @returns {Object} 图层控制器实例
|
|
276
|
-
*/
|
|
277
|
-
createLayer(layerType, options) {
|
|
278
|
-
// 使用图层管理器创建图层控制器实例
|
|
279
|
-
const layerController = this.layerManager.createLayer(layerType, options);
|
|
280
|
-
|
|
281
|
-
if (layerController) {
|
|
282
|
-
// 存储图层实例
|
|
283
|
-
this.layerInstances.set(
|
|
284
|
-
options?.config?.name || layerType,
|
|
285
|
-
layerController
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
// 初始化创建图层
|
|
289
|
-
layerController.createLayer(this).catch(err => {
|
|
290
|
-
console.error(
|
|
291
|
-
`[GdMapUtils Error]: Failed to create layer '${layerType}':`,
|
|
292
|
-
err
|
|
293
|
-
);
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
return layerController;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* 获取指定名称的图层实例
|
|
302
|
-
* @param {string} layerName - 图层名称
|
|
303
|
-
* @returns {Object|null} 图层控制器实例
|
|
304
|
-
*/
|
|
305
|
-
getLayer(layerName) {
|
|
306
|
-
return this.layerInstances.get(layerName);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* 移除指定名称的图层
|
|
311
|
-
* @param {string} layerName - 图层名称
|
|
312
|
-
* @returns {boolean} 是否移除成功
|
|
313
|
-
*/
|
|
314
|
-
removeLayer(layerName) {
|
|
315
|
-
const layerController = this.layerInstances.get(layerName);
|
|
316
|
-
|
|
317
|
-
if (layerController) {
|
|
318
|
-
// 隐藏图层
|
|
319
|
-
if (typeof layerController.hideLayer === 'function') {
|
|
320
|
-
layerController.hideLayer();
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// 从映射中移除
|
|
324
|
-
this.layerInstances.delete(layerName);
|
|
325
|
-
return true;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
return false;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* 获取所有图层实例
|
|
333
|
-
* @returns {Array} 图层控制器实例数组
|
|
334
|
-
*/
|
|
335
|
-
getAllLayers() {
|
|
336
|
-
return Array.from(this.layerInstances.values());
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* 显示指定名称的图层
|
|
341
|
-
* @param {string} layerName - 图层名称
|
|
342
|
-
* @returns {boolean} 是否显示成功
|
|
343
|
-
*/
|
|
344
|
-
showLayer(layerName) {
|
|
345
|
-
const layerController = this.layerInstances.get(layerName);
|
|
346
|
-
|
|
347
|
-
if (layerController && typeof layerController.showLayer === 'function') {
|
|
348
|
-
layerController.showLayer();
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
return false;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* 隐藏指定名称的图层
|
|
357
|
-
* @param {string} layerName - 图层名称
|
|
358
|
-
* @returns {boolean} 是否隐藏成功
|
|
359
|
-
*/
|
|
360
|
-
hideLayer(layerName) {
|
|
361
|
-
const layerController = this.layerInstances.get(layerName);
|
|
362
|
-
|
|
363
|
-
if (layerController && typeof layerController.hideLayer === 'function') {
|
|
364
|
-
layerController.hideLayer();
|
|
365
|
-
return true;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
return false;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
// 加载工具类方法到gdMapUtils中
|
|
372
|
-
Object.assign(GdMapUtils.prototype, {
|
|
373
|
-
...eventMixin,
|
|
374
|
-
...gdHelperMixin,
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
export default GdMapUtils;
|