tdt-map-vue2 0.1.3 → 0.1.4
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 -0
- package/lib/BaseMap.vue +51 -34
- package/lib/utils/controls.js +17 -13
- package/lib/utils/tianDiMapMarker.js +103 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,7 @@ Vue.use(TdtMap, {
|
|
|
59
59
|
| titleKeys | string | 否 | - | 指定 infos 中哪个数据有悬浮 title |
|
|
60
60
|
| infoBtn | {text:'xxx'} | 否 | - | 添加按钮,会触发全局的 mapClick 事件 |
|
|
61
61
|
| labelFn | function | 否 | - | 自定义 Label,返回{content,style,anchor},高优先级 |
|
|
62
|
+
| isCustomMarker | boolean | 否 | - | 是否自定义 marker,默认 false,为 true 时,会使用 initTianDiMapMarker 方法渲染 marker |
|
|
62
63
|
|
|
63
64
|
- labelFn 回调接受两个参数(data, point)。需要返回包含 content(渲染内容)、style(对应样式)、anchor(label 偏移量)的对象
|
|
64
65
|
|
package/lib/BaseMap.vue
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import gcoord from "gcoord";
|
|
10
10
|
import { installTMap } from "./utils/installMap";
|
|
11
11
|
import installControls from "./utils/controls";
|
|
12
|
+
import { initTianDiMapMarker } from "./utils/tianDiMapMarker";
|
|
12
13
|
import markerIcon from "./map1.svg";
|
|
13
14
|
|
|
14
15
|
export default {
|
|
@@ -77,6 +78,16 @@ export default {
|
|
|
77
78
|
type: Number,
|
|
78
79
|
default: 20,
|
|
79
80
|
},
|
|
81
|
+
positionControl: {
|
|
82
|
+
// 是否显示缩放平移控件
|
|
83
|
+
type: Boolean,
|
|
84
|
+
default: true,
|
|
85
|
+
},
|
|
86
|
+
zoomControl: {
|
|
87
|
+
// 是否显示比例尺控件
|
|
88
|
+
type: Boolean,
|
|
89
|
+
default: true,
|
|
90
|
+
},
|
|
80
91
|
},
|
|
81
92
|
|
|
82
93
|
data() {
|
|
@@ -178,7 +189,7 @@ export default {
|
|
|
178
189
|
mapInstance.disableInertia();
|
|
179
190
|
|
|
180
191
|
// 初始化控制器
|
|
181
|
-
installControls(mapInstance, TMap);
|
|
192
|
+
installControls({map:mapInstance, T:TMap,positionControl:this.positionControl,zoomControl:this.zoomControl});
|
|
182
193
|
mapInstance.addEventListener("click", this.removeRichInfo); // TODO
|
|
183
194
|
this.addEvents();
|
|
184
195
|
this.initAllMarkers(this.datas);
|
|
@@ -226,23 +237,10 @@ export default {
|
|
|
226
237
|
this.ifChangeViewEmit = false;
|
|
227
238
|
|
|
228
239
|
const bounds = this.map.getBounds(); // 获取地图可视区域
|
|
229
|
-
|
|
230
|
-
[bounds.kq.lng, bounds.kq.lat],
|
|
231
|
-
this.coordTrans?.to,
|
|
232
|
-
this.coordTrans?.from
|
|
233
|
-
);
|
|
234
|
-
const [lng_ne, lat_ne] = this.gcoordTransform(
|
|
235
|
-
[bounds.Lq.lng, bounds.Lq.lat],
|
|
236
|
-
this.coordTrans?.to,
|
|
237
|
-
this.coordTrans?.from
|
|
238
|
-
);
|
|
240
|
+
|
|
239
241
|
|
|
240
242
|
this.$emit("changeView", {
|
|
241
|
-
bounds
|
|
242
|
-
...bounds,
|
|
243
|
-
sw: { lng: lng_sw, lat: lat_sw },
|
|
244
|
-
ne: { lng: lng_ne, lat: lat_ne },
|
|
245
|
-
},
|
|
243
|
+
bounds,
|
|
246
244
|
zoom: this.map.getZoom(),
|
|
247
245
|
location,
|
|
248
246
|
});
|
|
@@ -264,6 +262,7 @@ export default {
|
|
|
264
262
|
|
|
265
263
|
// 分批次渲染管线
|
|
266
264
|
renderPipelinesInBatches(lines) {
|
|
265
|
+
console.log("----------renderPipelinesInBatches--------",lines);
|
|
267
266
|
if (!lines || lines.length === 0) return;
|
|
268
267
|
let pipelines = JSON.parse(JSON.stringify(lines));
|
|
269
268
|
this.removeAllPipelines();
|
|
@@ -276,15 +275,7 @@ export default {
|
|
|
276
275
|
);
|
|
277
276
|
const batch = pipelines.slice(batchIndex, endIndex);
|
|
278
277
|
batch.forEach((pipe) => {
|
|
279
|
-
const
|
|
280
|
-
(c) => new this.TMapGL.LngLat(c[0], c[1])
|
|
281
|
-
);
|
|
282
|
-
const polyline = new this.TMapGL.Polyline(lngLatArr, {
|
|
283
|
-
color: pipe.color,
|
|
284
|
-
weight: 3,
|
|
285
|
-
opacity: 0.7,
|
|
286
|
-
lineJoin: "round",
|
|
287
|
-
});
|
|
278
|
+
const polyline = this.getLine(pipe);
|
|
288
279
|
this.map.addOverLay(polyline);
|
|
289
280
|
// 存储管线图层,便于后续操作
|
|
290
281
|
pipe.layer = polyline;
|
|
@@ -295,12 +286,30 @@ export default {
|
|
|
295
286
|
// 异步渲染下一批,避免阻塞主线程
|
|
296
287
|
requestAnimationFrame(renderBatch);
|
|
297
288
|
} else {
|
|
298
|
-
console.log("所有管线渲染完成,总数:", pipelines.length);
|
|
299
|
-
this.$emit("renderLine");
|
|
289
|
+
console.log("所有管线渲染完成,总数:", pipelines.length,pipelines);
|
|
290
|
+
this.$emit("renderLine",this.pipelineLayers);
|
|
300
291
|
}
|
|
301
292
|
};
|
|
302
293
|
renderBatch();
|
|
303
294
|
},
|
|
295
|
+
getLine(pipe) {
|
|
296
|
+
const lngLatArr = pipe.points.map(
|
|
297
|
+
(c) => {
|
|
298
|
+
const coord = this.gcoordTransform(c)
|
|
299
|
+
return new this.TMapGL.LngLat(...coord)
|
|
300
|
+
}
|
|
301
|
+
);
|
|
302
|
+
console.log('--------getLine------',pipe,lngLatArr);
|
|
303
|
+
|
|
304
|
+
const polyline = new this.TMapGL.Polyline(lngLatArr, pipe.lineOptions);
|
|
305
|
+
polyline.addEventListener("mouseover", (e) => {
|
|
306
|
+
this.$emit("lineMouseover", { line:polyline,data:pipe, e });
|
|
307
|
+
});
|
|
308
|
+
polyline.addEventListener("mouseout", (e) => {
|
|
309
|
+
this.$emit("lineMouseout", { line:polyline,data:pipe, e });
|
|
310
|
+
});
|
|
311
|
+
return polyline;
|
|
312
|
+
},
|
|
304
313
|
|
|
305
314
|
// 清空所有管线
|
|
306
315
|
removeAllPipelines() {
|
|
@@ -362,13 +371,20 @@ export default {
|
|
|
362
371
|
const point = new this.TMapGL.LngLat(...result);
|
|
363
372
|
|
|
364
373
|
if (data.labelFn) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
374
|
+
if(data.isCustomMarker) {
|
|
375
|
+
marker = initTianDiMapMarker({
|
|
376
|
+
map: this.map,
|
|
377
|
+
data,
|
|
378
|
+
point,
|
|
379
|
+
})
|
|
380
|
+
}else {
|
|
381
|
+
const labelRes = data.labelFn(data, point);
|
|
382
|
+
const label = new this.TMapGL.Label({
|
|
383
|
+
text: labelRes.content,
|
|
384
|
+
position: point,
|
|
385
|
+
offset: new this.TMapGL.Point(...(labelRes.anchor || [0, 0])),
|
|
386
|
+
});
|
|
387
|
+
marker = label;}
|
|
372
388
|
} else {
|
|
373
389
|
const icon = new this.TMapGL.Icon({
|
|
374
390
|
iconUrl: data.img || markerIcon,
|
|
@@ -376,6 +392,7 @@ export default {
|
|
|
376
392
|
});
|
|
377
393
|
marker = new this.TMapGL.Marker(point, { icon });
|
|
378
394
|
}
|
|
395
|
+
if(data.noClick) return
|
|
379
396
|
|
|
380
397
|
marker.addEventListener("click", (e) => {
|
|
381
398
|
setTimeout(() => {
|
package/lib/utils/controls.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
export default (map, T) => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
scale.
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
export default ({map, T, positionControl = true, zoomControl = true}) => {
|
|
2
|
+
if(positionControl) {
|
|
3
|
+
// 创建缩放平移控件
|
|
4
|
+
const control = new T.Control.Zoom()
|
|
5
|
+
map.addControl(control)
|
|
6
|
+
const controlPosition = window.T_ANCHOR_BOTTOM_RIGHT
|
|
7
|
+
control.setPosition(controlPosition)
|
|
8
|
+
}
|
|
9
|
+
if(zoomControl) {
|
|
10
|
+
// 创建比例尺控件
|
|
11
|
+
const scale = new T.Control.Scale({ position: window.T_ANCHOR_BOTTOM_RIGHT })
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
scale.setOffset({ x: -60, y: 60 })
|
|
14
|
+
}, 0)
|
|
15
|
+
map.addControl(scale)
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// 天地图自定义覆盖物实现方案
|
|
2
|
+
// 对应百度地图的 BMapGL.CustomOverlay 功能
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 创建天地图自定义覆盖物
|
|
6
|
+
* @param {Object} options - 配置项
|
|
7
|
+
* @param {Object} options.map - 天地图实例
|
|
8
|
+
* @param {Array} options.data - 覆盖物数据
|
|
9
|
+
* @param {Array} options.point - marker坐标
|
|
10
|
+
* @param {Function} options.data.labelFn - 生成DOM的函数
|
|
11
|
+
* @param {Array} options.data.anchor - 锚点偏移量
|
|
12
|
+
* @returns {Object} 自定义覆盖物实例
|
|
13
|
+
*/
|
|
14
|
+
export function initTianDiMapMarker({
|
|
15
|
+
map,
|
|
16
|
+
data,
|
|
17
|
+
point
|
|
18
|
+
}) {
|
|
19
|
+
if (!data.lng || !data.lat) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 创建自定义覆盖物类
|
|
24
|
+
const CustomOverlay = T.Overlay.extend({
|
|
25
|
+
// 初始化方法
|
|
26
|
+
initialize: function(map) {
|
|
27
|
+
console.log("----------initialize----------------",map);
|
|
28
|
+
this.map = map;
|
|
29
|
+
this.lnglat = point;
|
|
30
|
+
this.data = data;
|
|
31
|
+
// return this.onAdd(map);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
onAdd: function(map) {
|
|
35
|
+
console.log("----------onAdd----------------");
|
|
36
|
+
// 创建DOM元素
|
|
37
|
+
const div = (this._div = document.createElement('div'));
|
|
38
|
+
div.style.position = 'absolute';
|
|
39
|
+
div.style.zIndex = 1000;
|
|
40
|
+
|
|
41
|
+
// 生成自定义DOM内容
|
|
42
|
+
if (this.data.labelFn) {
|
|
43
|
+
const domElement = this.data.labelFn(this.data);
|
|
44
|
+
div.appendChild(domElement);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 添加到覆盖物层
|
|
48
|
+
map.getPanes().overlayPane.appendChild(div);
|
|
49
|
+
|
|
50
|
+
// 执行绘制
|
|
51
|
+
this.update();
|
|
52
|
+
|
|
53
|
+
return div;
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
onRemove: function () {
|
|
57
|
+
console.log("----------onRemove----------------",this._div);
|
|
58
|
+
var parent = this._div.parentNode;
|
|
59
|
+
if (parent) {
|
|
60
|
+
parent.removeChild(this._div);
|
|
61
|
+
this.map = null;
|
|
62
|
+
this._div = null;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
// 绘制方法
|
|
67
|
+
update: function() {
|
|
68
|
+
console.log("----------update----------------",this.lnglat,this.data);
|
|
69
|
+
// 获取地理坐标转换为容器像素坐标
|
|
70
|
+
const pixel = this.map.lngLatToLayerPoint(this.lnglat);
|
|
71
|
+
const anchor = this.data.anchor || [0, 0];
|
|
72
|
+
console.log("----------draw----------------",this.lnglat,pixel,anchor);
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
// 设置DOM元素位置
|
|
76
|
+
this._div.style.left = (pixel.x + anchor[0]) + 'px';
|
|
77
|
+
this._div.style.top = (pixel.y + anchor[1]) + 'px';
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 创建自定义覆盖物实例
|
|
82
|
+
const customOverlay = new CustomOverlay(map);
|
|
83
|
+
|
|
84
|
+
return customOverlay;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
// 默认导出
|
|
89
|
+
export default ({
|
|
90
|
+
map,
|
|
91
|
+
data,
|
|
92
|
+
point
|
|
93
|
+
}) => {
|
|
94
|
+
const overlay = initTianDiMapMarker({
|
|
95
|
+
map,
|
|
96
|
+
data,
|
|
97
|
+
point
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
overlay
|
|
102
|
+
};
|
|
103
|
+
};
|