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/src/index.html DELETED
@@ -1,14 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="zh-CN">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title><%= htmlWebpackPlugin.options.title %></title>
7
- </head>
8
- <body>
9
- <h1>地图工具库演示</h1>
10
- <div id="app">
11
- <p>使用 F12 打开控制台,查看 MapUtils 的使用示例。</p>
12
- </div>
13
- </body>
14
- </html>
@@ -1,30 +0,0 @@
1
- class MarkerClusterLayer {
2
- rawLayer = new AMap.OverlayGroup();
3
-
4
- constructor() {}
5
-
6
- createOverlays() {
7
- return [];
8
- }
9
- bindEventMarker(clickType: AMap.EventType, callback: () => void) {}
10
-
11
- add(markers: Array<AMap.Marker>) {}
12
-
13
- remove(markers: Array<AMap.Marker>) {}
14
-
15
- highlightOverLay() {}
16
-
17
- hide() {}
18
-
19
- show() {}
20
-
21
- getAllOverlay() {}
22
-
23
- destroy() {}
24
-
25
- reload() {}
26
-
27
- overlayFitMap() {}
28
- }
29
-
30
- export default MarkerClusterLayer;
@@ -1,122 +0,0 @@
1
- import useEnvSanStore from '@/store/modules/envSan.js';
2
- import GdMapUtils from '@/utils/gdMap/gdMapUtils.js';
3
-
4
- // 海量点图层渲染封装
5
- export default class LabelMarkerLayer {
6
- dataList = []; // 数据列表
7
-
8
- // 去掉私有属性标识
9
- layerInstance = null; // 图层实例
10
-
11
- // 去掉私有属性标识
12
- isLayerCreated = false; // 图层是否已创建
13
-
14
- envSanStore = useEnvSanStore(); // 使用环境状态存储
15
-
16
- activeNames = []; // 存储激活图层显示name
17
- /**
18
- * 经纬度坐标,用来描述地图上的一个点位置
19
- * @param {Object} config 图层的config
20
- * @param {Function} createOverlay 创建marker的方法
21
- * @param {Function} noWrap requestCallback 拉去marker请求数据并按规定格式返回
22
- * @param {Boolean} detectingPosition 是否检测位置变化
23
- */
24
- constructor({ config, createOverlay, requestCallback }) {
25
- this.config = config ?? {}; //保存图层配置
26
-
27
- this.createOverlay = createOverlay;
28
-
29
- this.requestCallback = requestCallback;
30
-
31
- this.activeNames = [
32
- ...(this?.config?.extraActiveName ?? []),
33
- this.config.name,
34
- ]; //图层额外的激活数组
35
- }
36
-
37
- // 获取地图工具类实例
38
- getGdMapUtilsIns(id = 'gisMap') {
39
- return GdMapUtils.mapInstance.get(id);
40
- }
41
-
42
- // 创建图层
43
- async createLayer(gdMapUtils) {
44
- // 获取数据
45
- this.dataList = await this.requestCallback();
46
-
47
- if (!this.shouldSkipLayerCreation) return; //避免网络时间过长用户切换到切tab
48
-
49
- // 创建地图图层
50
- this.layerInstance = gdMapUtils.createLabelLayer(this.config.layerOptions);
51
-
52
- // 创建标记
53
- let markers = this.dataList.map(item => {
54
- return this.createOverlay(gdMapUtils, this.config, item);
55
- });
56
-
57
- this.layerInstance.add(markers); // 添加标记到图层
58
- //HACK labelLayer没有统一绑定事件的方法
59
- markers.forEach(overlay => {
60
- overlay.on('click', e => {
61
- const marker = e.target;
62
-
63
- if (marker.getExtData().type === this.config.className) {
64
- // 触发全局的地图弹框
65
- gdMapUtils.trigger(
66
- 'pointerClick',
67
- marker,
68
- e,
69
- gdMapUtils.map,
70
- this.config
71
- );
72
- }
73
- });
74
- });
75
-
76
- this.isLayerCreated = true; // 设置图层创建状态为true
77
-
78
- markers = null; // 释放内存
79
- }
80
-
81
- // 显示图层
82
- showLayer() {
83
- if (
84
- this.layerInstance &&
85
- this.dataList.length &&
86
- this.shouldSkipLayerCreation
87
- ) {
88
- this.layerInstance.show(); // 显示图层
89
- }
90
- }
91
- get shouldSkipLayerCreation() {
92
- return this.activeNames.includes(this.envSanStore.mapActiveType);
93
- }
94
-
95
- // 隐藏图层
96
- hideLayer() {
97
- if (this.layerInstance && this.dataList.length) {
98
- this.layerInstance.hide(); // 隐藏图层
99
- }
100
- }
101
-
102
- // 监听地图类型变化
103
- handleMapTypeChange(newVal, oldVal) {
104
- let gdMapUtils = this.getGdMapUtilsIns(); // 获取地图实例
105
-
106
- if (!gdMapUtils) return; // 如果地图实例不存在,则不执行后续操作
107
-
108
- if (this.shouldSkipLayerCreation) {
109
- if (this.isLayerCreated) {
110
- this.showLayer(); // 显示图层
111
- } else {
112
- this.createLayer(gdMapUtils); // 创建图层
113
- }
114
- } else {
115
- this.hideLayer(); // 隐藏图层
116
- }
117
- }
118
-
119
- get dataOfLayer() {
120
- return this.dataList;
121
- }
122
- }
@@ -1,155 +0,0 @@
1
- // import useEnvSanStore from "@/store/modules/envSan.js";
2
- import GdMapUtils from '@/utils/gdMap/gdMapUtils.js';
3
-
4
- /*
5
- CollectionPointLayerController
6
- */
7
- // 海量点图层渲染封装
8
- export default class MarkerClusterLayer {
9
- dataList = []; // 数据列表
10
-
11
- // 去掉私有属性标识
12
- layerInstance = null; // 图层实例
13
-
14
- // 去掉私有属性标识
15
- isLayerCreated = false; // 图层是否已创建
16
-
17
- activeNames = []; // 存储激活图层显示name
18
- /**
19
- * 经纬度坐标,用来描述地图上的一个点位置
20
- * @param {Object} config 图层的config
21
- * @param {Function} createOverlay 创建marker的方法
22
- * @param {Function} noWrap requestCallback 拉去marker请求数据并按规定格式返回
23
- * @param {Boolean} detectingPosition 是否检测位置变化
24
- */
25
- constructor({ config, createOverlay, requestCallback }) {
26
- this.config = config ?? {}; //保存图层配置
27
-
28
- this.createOverlay = createOverlay;
29
-
30
- this.requestCallback = requestCallback;
31
-
32
- this.activeNames = [
33
- ...(this?.config?.extraActiveName ?? []),
34
- this.config.name,
35
- ]; //图层额外的激活数组
36
- }
37
-
38
- // 获取地图工具类实例
39
- getGdMapUtilsIns(id = 'gisMap') {
40
- return GdMapUtils.mapInstance.get(id);
41
- }
42
-
43
- // 创建图层
44
- async createLayer(gdMapUtils) {
45
- // 获取数据
46
- this.dataList = await this.requestCallback();
47
-
48
- const { dataList, config } = this; //保存数据
49
-
50
- if (!this.shouldCreationLayer(config.name)) return; //避免网络时间过长用户切换到切tab
51
-
52
- // 创建海量点渲染
53
- this.layerInstance = gdMapUtils.createMarkerCluster(dataList, {
54
- gridSize: 80,
55
- _renderClusterMarker(context) {
56
- // 绘制聚合点时调用
57
- const count = dataList.length;
58
- const factor = Math.pow(context.count / count, 1 / 18);
59
- const div = document.createElement('div');
60
- const Hue = 180 - factor * 180;
61
- const bgColor = 'hsla(' + Hue + ',100%,50%,0.7)';
62
- const fontColor = 'hsla(' + Hue + ',100%,20%,1)';
63
- const borderColor = 'hsla(' + Hue + ',100%,40%,1)';
64
- const shadowColor = 'hsla(' + Hue + ',100%,50%,1)';
65
- div.style.backgroundColor = bgColor;
66
- const size = Math.round(
67
- 30 + Math.pow(context.count / count, 1 / 5) * 20
68
- );
69
- div.style.width = div.style.height = size + 'px';
70
- div.style.border = 'solid 1px ' + borderColor;
71
- div.style.borderRadius = size / 2 + 'px';
72
- div.style.boxShadow = '0 0 1px ' + shadowColor;
73
- div.innerHTML = context.count;
74
- div.style.lineHeight = size + 'px';
75
- div.style.color = fontColor;
76
- div.style.fontSize = '14px';
77
- div.style.textAlign = 'center';
78
- const Pixel = gdMapUtils.Size(-size / 2, -size / 2);
79
- context.marker.setOffset(Pixel);
80
- context.marker.setContent(div);
81
- }, // 自定义聚合点样式
82
- _renderMarker: context => {
83
- // 外部控制单个marker的样式
84
- this.createOverlay({ context, gdMapUtils, config }); // 创建marker
85
- },
86
- });
87
-
88
- // 绑定监听控制label显示
89
- this.layerInstance.on('click', e => {
90
- const { lnglat, marker, clusterData } = e;
91
-
92
- if (clusterData.length > 1) {
93
- //点击集合样式地图放大一级
94
- gdMapUtils.setCenter(lnglat, false);
95
- gdMapUtils.map.zoomIn(); // 放大地图
96
- }
97
-
98
- if (marker instanceof AMap.Marker) {
99
- // marker?.dom?.querySelector('.sydw-label').classList.remove('display-none');
100
- gdMapUtils.trigger(
101
- 'pointerClick',
102
- marker,
103
- e,
104
- gdMapUtils.map,
105
- this.config
106
- );
107
- }
108
- });
109
-
110
- this.isLayerCreated = true; // 设置图层创建状态为true
111
- }
112
-
113
- // 显示图层
114
- showLayer(v) {
115
- if (
116
- this.layerInstance &&
117
- this.dataList.length &&
118
- this.shouldCreationLayer(v)
119
- ) {
120
- this.layerInstance.setData(this.dataList);
121
- }
122
- }
123
- // 判断是否跳过图层创建
124
- shouldCreationLayer(activeName) {
125
- return this.activeNames.includes(activeName);
126
- }
127
-
128
- // 隐藏图层
129
- hideLayer() {
130
- if (this.layerInstance && this.dataList.length) {
131
- this.layerInstance.setData([]);
132
- }
133
- }
134
-
135
- // 监听地图类型变化
136
- handleMapTypeChange(newVal, oldVal) {
137
- let gdMapUtils = this.getGdMapUtilsIns(); // 获取地图实例
138
-
139
- if (!gdMapUtils) return; // 如果地图实例不存在,则不执行后续操作
140
-
141
- if (this.shouldCreationLayer(newVal)) {
142
- if (this.isLayerCreated) {
143
- this.showLayer(newVal); // 显示图层
144
- } else {
145
- this.createLayer(gdMapUtils); // 创建图层
146
- }
147
- } else {
148
- this.hideLayer(); // 隐藏图层
149
- }
150
- }
151
-
152
- get dataOfLayer() {
153
- return this.dataList;
154
- }
155
- }
@@ -1,267 +0,0 @@
1
- import GdMapUtils from '@/utils/gdMap/gdMapUtils.js';
2
-
3
- export default class MarkerLayer {
4
- dataList = []; // 数据列表
5
-
6
- // 去掉私密属性标识
7
- updatePointerTimer = null;
8
-
9
- layerInstance = null; // 图层实例
10
-
11
- isLayerCreated = false; // 图层是否已创建
12
-
13
- detectingPosition = null; //检测位置的变化
14
-
15
- activeNames = [];
16
- /**
17
- * 经纬度坐标,用来描述地图上的一个点位置
18
- * @param {Object} config 图层的config
19
- * @param {Function} createOverlay 创建marker的方法
20
- * @param {Function} noWrap requestCallback 拉去marker请求数据并按规定格式返回
21
- * @param {Boolean} detectingPosition 是否检测位置变化
22
- */
23
- constructor({
24
- //!没有文档还需要了解内部细节,耗费时间精力
25
- config,
26
- createOverlay,
27
- requestCallback,
28
- detectingPosition = false,
29
- }) {
30
- this.config = config ?? {}; //保存图层配置
31
-
32
- this.createOverlay = createOverlay;
33
-
34
- this.requestCallback = requestCallback; //
35
-
36
- this.detectingPosition = detectingPosition; // 是否检测位置变化
37
-
38
- this.activeNames = [
39
- ...(this?.config?.extraActiveName ?? []),
40
- this.config.name,
41
- ]; //! 图层啥时候激活
42
-
43
- // 添加页面可见性变化监听
44
- document.addEventListener(
45
- 'visibilitychange',
46
- this.handleVisibilityChange.bind(this)
47
- );
48
- }
49
-
50
- // 获取地图工具类实例
51
- getGdMapUtilsIns(id = 'gisMap') {
52
- return GdMapUtils.mapInstance.get(id);
53
- }
54
-
55
- // 处理页面可见性变化
56
- handleVisibilityChange() {
57
- const gdMapUtils = this.getGdMapUtilsIns();
58
- if (document.hidden) {
59
- // 页面隐藏时停止检测
60
- this.stopDetectingPositionChange();
61
- } else {
62
- // 页面显示时启动检测
63
- if (this.detectingPosition && this.layerInstance) {
64
- this.startDetectingPositionChange(gdMapUtils);
65
- }
66
- }
67
- }
68
-
69
- // 创建图层
70
- async createLayer(gdMapUtils) {
71
- // 获取数据
72
- this.dataList = await this.requestCallback();
73
-
74
- if (!this.shouldCreationLayer(this.config.name)) return; // 接口请求缓慢,避免用户切换菜单
75
-
76
- // 处理数据
77
- this.dataList.forEach(item => {
78
- this.createOverlay(gdMapUtils, this.config, item);
79
- });
80
-
81
- this.layerInstance = gdMapUtils.getOverlayGroupManager(
82
- this.config.className
83
- ); // 获取图层对象
84
-
85
- const markers = this.layerInstance.OverlayGroup.getOverlays();
86
- //HACK 名字不够可读
87
- gdMapUtils.trigger('markerShowed', markers);
88
-
89
- //! 不需要响应click的marker如何处理
90
- gdMapUtils.bindEventMarker(this.config.className, 'click', e => {
91
- const marker = e.target;
92
-
93
- if (marker.getExtData().type === this.config.className) {
94
- this.layerInstance.resetActiveMarker(); // 重置激活的标记
95
- this.layerInstance.setActiveMarker(marker); // 设置激活的标记
96
- marker.setzIndex(1001);
97
- gdMapUtils.trigger(
98
- 'pointerClick',
99
- marker,
100
- e,
101
- gdMapUtils.map,
102
- this.config
103
- );
104
- }
105
- });
106
-
107
- // 检测车辆经纬度是否发生变化
108
- this.startDetectingPositionChange(gdMapUtils);
109
-
110
- this.isLayerCreated = true; // 设置图层显示状态为true
111
- }
112
-
113
- // 显示图层
114
- showLayer() {
115
- if (
116
- this.layerInstance &&
117
- this.dataList.length &&
118
- this.shouldCreationLayer()
119
- ) {
120
- this.layerInstance.showOverlay(); // 显示图层
121
- }
122
- }
123
-
124
- // 隐藏图层
125
- hideLayer() {
126
- if (this.layerInstance && this.dataList.length) {
127
- this.layerInstance.hideOverlay(); // 隐藏图层
128
- }
129
- }
130
-
131
- // 手动触发高亮某个点位
132
- highlightMarker(id) {
133
- const layerInstance = this.layerInstance;
134
-
135
- if (!layerInstance) return; // 如果图层不存在,则不执行后续操作
136
-
137
- const marker = layerInstance.findLayerMarker(id);
138
- if (marker) {
139
- layerInstance.resetActiveMarker(); // 重置激活的标记
140
- layerInstance.setActiveMarker(marker); // 设置激活的标记
141
- marker.setzIndex(1001); // 设置zIndex
142
- }
143
- }
144
-
145
- // 由get 访问器描述符 相较于getter函数不能传递更多参数
146
- shouldCreationLayer(activeName = this.config.name) {
147
- return this.activeNames.includes(activeName);
148
- }
149
- // 启动检测车辆经纬度变化
150
- startDetectingPositionChange(getGdMapUtilsIns) {
151
- if (!this.layerInstance || !this.detectingPosition) return;
152
- this.stopDetectingPositionChange(); //先停止在开启,避免多次执行
153
- // 修改定时器属性引用
154
- this.updatePointerTimer = setInterval(
155
- () => this.updatePointer(getGdMapUtilsIns),
156
- this.config.updateTime
157
- );
158
- }
159
-
160
- // 停止检测车辆经纬度变化
161
- stopDetectingPositionChange() {
162
- // 修改定时器属性引用
163
- clearInterval(this.updatePointerTimer);
164
- }
165
-
166
- // 更新车辆位置
167
- async updatePointer(getGdMapUtilsIns) {
168
- if (!this.layerInstance) return; // 如果图层不存在,则不执行后续操作
169
- // 获取车辆数据
170
- const newestDataList = await this.requestCallback();
171
-
172
- if (this.config.name !== this.envSanStore.mapActiveType) return; // 接口请求缓慢,避免用户切换菜单
173
-
174
- // 比较新旧数据,找出需要更新的标记
175
- const changedData = this.differenceWith(newestDataList, this.dataList);
176
-
177
- changedData.forEach(item => {
178
- const marker = this.layerInstance.findLayerMarker(item.id);
179
-
180
- const iconImage = item.extData.onLine
181
- ? this.config.onLineIcon
182
- : this.config.icon;
183
- // 激活图标不需要更新位置和图标
184
- const activesMarkerIds = this.layerInstance.activesMarkerIds; // 获取激活的markerId
185
-
186
- if (!activesMarkerIds.includes(item.id)) {
187
- if (marker) {
188
- //存在的marker需要更新位置和图标
189
-
190
- marker.setPosition(getGdMapUtilsIns.LngLat(item.jd, item.wd));
191
-
192
- const icon = getGdMapUtilsIns.createIcon(
193
- this.config.size,
194
- iconImage,
195
- this.config.size
196
- );
197
-
198
- marker.setIcon(icon);
199
- } else {
200
- //没有的marker需要重新创建
201
- this.createOverlay(getGdMapUtilsIns, iconImage, item);
202
- }
203
- }
204
- });
205
-
206
- this.dataList = newestDataList; // 更新数据列表
207
- }
208
-
209
- // 比较新旧数据,找出经纬度发生变化的项
210
- differenceWith(newData, oldData) {
211
- return newData.filter(nItem => {
212
- const oldItem = oldData.find(oItem => oItem.id === nItem.id); // 没有直接返回
213
- return !oldItem || nItem.jd !== oldItem.jd || nItem.wd !== oldItem.wd;
214
- });
215
- }
216
-
217
- // 处理地图类型变化
218
- handleMapTypeChange(newVal, oldVal, id) {
219
- const gdMapUtils = this.getGdMapUtilsIns(id); // 获取地图实例
220
-
221
- if (!gdMapUtils) return; // 如果地图实例不存在,则不执行后续操作
222
- if (this.shouldCreationLayer(newVal)) {
223
- if (this.isLayerCreated) {
224
- this.showLayer(); // 显示图层
225
- } else {
226
- this.createLayer(gdMapUtils); // 创建图层
227
- }
228
- } else {
229
- this.hideLayer(); // 隐藏图层
230
- }
231
-
232
- // 离开中转页时,停止检测车辆经纬度变化
233
- if (this.shouldCreationLayer(oldVal) && !this.shouldCreationLayer(newVal)) {
234
- this.stopDetectingPositionChange();
235
- }
236
-
237
- // 进入中转页时,启动检测车辆经纬度变化
238
- if (this.shouldCreationLayer(newVal) && !this.shouldCreationLayer(oldVal)) {
239
- this.startDetectingPositionChange(gdMapUtils);
240
- }
241
- }
242
-
243
- // 销毁实例时移除事件监听
244
- destroy() {
245
- document.removeEventListener(
246
- 'visibilitychange',
247
- this.handleVisibilityChange.bind(this)
248
- );
249
-
250
- this.layerInstance.OverlayGroup.clearOverlays();
251
- }
252
-
253
- // 获取所有marker
254
- get markerList() {
255
- //HACK 暂时注释掉
256
- if (!this.layerInstance) return;
257
-
258
- const markers = this.layerInstance.OverlayGroup.getOverlays();
259
-
260
- return markers;
261
- }
262
-
263
- get dataOfLayer() {
264
- return this.dataList;
265
- }
266
- }
267
- // 扩展事件