xy-map 1.0.0
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/compontents/detail-dialog.vue +141 -0
- package/image/arrow_white.png +0 -0
- package/image/map_end.png +0 -0
- package/image/map_start.png +0 -0
- package/image/marker.png +0 -0
- package/layers/Line.js +175 -0
- package/layers/Point.js +251 -0
- package/layers/Polygon.js +94 -0
- package/layers/Text.js +46 -0
- package/layers/index.js +119 -0
- package/map.js +400 -0
- package/package.json +13 -0
- package/style/hoverHtml.css +29 -0
- package/style/map.css +7 -0
- package/util/mapEvent.js +108 -0
- package/util/mapHoverHtml.js +54 -0
- package/util/mapTools.js +117 -0
- package/util/measure-area.js +215 -0
- package/util/measure-distance.js +600 -0
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import mapboxgl from 'mapbox-gl'
|
|
2
|
+
import * as turf from '@turf/turf'
|
|
3
|
+
|
|
4
|
+
const initData = {
|
|
5
|
+
clickMeasurePointsFunction: null,
|
|
6
|
+
mapClickFunction: null,
|
|
7
|
+
mapMousemoveFunction: null,
|
|
8
|
+
mapDblClickFunction: null,
|
|
9
|
+
map: null,
|
|
10
|
+
isMeasure: true,
|
|
11
|
+
jsonPoint: {
|
|
12
|
+
'type': 'FeatureCollection',
|
|
13
|
+
'features': []
|
|
14
|
+
},
|
|
15
|
+
jsonLine: {
|
|
16
|
+
'type': 'FeatureCollection',
|
|
17
|
+
'features': []
|
|
18
|
+
},
|
|
19
|
+
points: []
|
|
20
|
+
}
|
|
21
|
+
let before = null
|
|
22
|
+
let after = null
|
|
23
|
+
|
|
24
|
+
let textColor = '#fff'
|
|
25
|
+
let lineColor = '#f00'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 测距
|
|
29
|
+
* @param mapObject
|
|
30
|
+
*/
|
|
31
|
+
export function measureLineLength(mapObject) {
|
|
32
|
+
const currentMapContainerId = mapObject._container.getAttribute('id')
|
|
33
|
+
currentMapContainerId.indexOf('-after') !== -1 ? after = JSON.parse(JSON.stringify(initData)) : before = JSON.parse(JSON.stringify(initData))
|
|
34
|
+
const resultData = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
35
|
+
resultData.isMeasure = true
|
|
36
|
+
resultData.map = mapObject
|
|
37
|
+
resultData.map.doubleClickZoom.disable() // 禁止双击缩放
|
|
38
|
+
resultData.map.getCanvas().style.cursor = 'default'
|
|
39
|
+
clearMeasureLine(mapObject)
|
|
40
|
+
resultData.jsonPoint = {
|
|
41
|
+
'type': 'FeatureCollection',
|
|
42
|
+
'features': []
|
|
43
|
+
}
|
|
44
|
+
resultData.jsonLine = {
|
|
45
|
+
'type': 'FeatureCollection',
|
|
46
|
+
'features': []
|
|
47
|
+
}
|
|
48
|
+
resultData.points = []
|
|
49
|
+
const {
|
|
50
|
+
mouseLabel,
|
|
51
|
+
ele
|
|
52
|
+
} = createMeasurLineLabelMarkerHandler(mapObject)
|
|
53
|
+
createMeasureLinePLLHandler(resultData.jsonPoint, resultData.jsonLine, mapObject)
|
|
54
|
+
|
|
55
|
+
let timer = null
|
|
56
|
+
|
|
57
|
+
function mapClickHandler(_e) {
|
|
58
|
+
timer = setTimeout(() => {
|
|
59
|
+
clearTimeout(timer)
|
|
60
|
+
if (resultData.isMeasure) {
|
|
61
|
+
const coords = [_e.lngLat.lng, _e.lngLat.lat]
|
|
62
|
+
addMeasureLineRes(resultData.points, resultData.jsonPoint, coords, mapObject)
|
|
63
|
+
addMeasureLinePoint(resultData.jsonPoint, resultData.jsonLine, coords, mapObject)
|
|
64
|
+
resultData.points.push(coords)
|
|
65
|
+
}
|
|
66
|
+
}, 100)
|
|
67
|
+
}
|
|
68
|
+
resultData.mapClickFunction = mapClickHandler
|
|
69
|
+
resultData.map.off('click', mapClickHandler)
|
|
70
|
+
resultData.map.on('click', mapClickHandler)
|
|
71
|
+
|
|
72
|
+
function mousemoveHandler(_e) {
|
|
73
|
+
if (resultData.isMeasure) {
|
|
74
|
+
const coords = [_e.lngLat.lng, _e.lngLat.lat]
|
|
75
|
+
if (resultData.jsonPoint.features.length > 0) {
|
|
76
|
+
const prev = resultData.jsonPoint.features[resultData.jsonPoint.features.length - 1]
|
|
77
|
+
const json = {
|
|
78
|
+
type: 'Feature',
|
|
79
|
+
geometry: {
|
|
80
|
+
type: 'LineString',
|
|
81
|
+
coordinates: [prev.geometry.coordinates, coords]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
resultData.map.getSource('measure-line-move').setData(json)
|
|
85
|
+
let resultText = '起点'
|
|
86
|
+
if (resultData.points.length !== 0) {
|
|
87
|
+
// const prev = resultData.jsonPoint.features[resultData.jsonPoint.features.length - 1]
|
|
88
|
+
// const prevCoordinates = prev ? prev.geometry.coordinates : ''
|
|
89
|
+
// const resultAngle = prevCoordinates ? getAngleHandle(prevCoordinates[0], prevCoordinates[1], coords[0], coords[1]).toFixed(1) + '°' : ''
|
|
90
|
+
// resultText = `<span>${getMeasureLineLength(resultData.points, coords)} / ${resultAngle}</span> <span>单击确定地点,双击结束</span>`
|
|
91
|
+
resultText = `<span>${getMeasureLineLength(resultData.points, coords)}</span> <span>单击确定地点,双击结束</span>`
|
|
92
|
+
}
|
|
93
|
+
ele.innerHTML = resultText
|
|
94
|
+
} else {
|
|
95
|
+
ele.style.display = 'block'
|
|
96
|
+
ele.style.color = textColor
|
|
97
|
+
ele.innerHTML = '点击地图开始测量'
|
|
98
|
+
}
|
|
99
|
+
mouseLabel.setLngLat(coords)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
resultData.mapMousemoveFunction = mousemoveHandler
|
|
103
|
+
resultData.map.off('mousemove', mousemoveHandler)
|
|
104
|
+
resultData.map.on('mousemove', mousemoveHandler)
|
|
105
|
+
|
|
106
|
+
function dblclickHandler(_e) {
|
|
107
|
+
if (timer) {
|
|
108
|
+
clearTimeout(timer)
|
|
109
|
+
}
|
|
110
|
+
if (resultData.isMeasure) {
|
|
111
|
+
const coords = [_e.lngLat.lng, _e.lngLat.lat]
|
|
112
|
+
addMeasureLinePoint(resultData.jsonPoint, resultData.jsonLine, coords, mapObject)
|
|
113
|
+
resultData.isMeasure = false
|
|
114
|
+
resultData.map.getCanvas().style.cursor = ''
|
|
115
|
+
mouseLabel.remove()
|
|
116
|
+
// 当前已关闭点击测距,所以清除move的线段
|
|
117
|
+
resultData.map.getSource('measure-line-move').setData({
|
|
118
|
+
'type': 'FeatureCollection',
|
|
119
|
+
'features': []
|
|
120
|
+
})
|
|
121
|
+
// 鼠标移入当前测距的点位,动态显示tips提示语
|
|
122
|
+
resultData.map.on('mouseover', 'measure-line-points', (event) => {
|
|
123
|
+
if (resultData.isMeasure) {
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
resultData.map.getCanvas().style.cursor = 'pointer'
|
|
127
|
+
const tipsCoords = [event.lngLat.lng, event.lngLat.lat]
|
|
128
|
+
const tipsEle = document.createElement('div')
|
|
129
|
+
tipsEle.setAttribute('class', 'measure-line-result delete-tips white')
|
|
130
|
+
tipsEle.innerHTML = '单击可删除此点'
|
|
131
|
+
const tipsOption = {
|
|
132
|
+
element: tipsEle,
|
|
133
|
+
anchor: 'left',
|
|
134
|
+
offset: [10, 20]
|
|
135
|
+
}
|
|
136
|
+
// eslint-disable-next-line no-undef
|
|
137
|
+
new mapboxgl.Marker(tipsOption)
|
|
138
|
+
.setLngLat(tipsCoords)
|
|
139
|
+
.addTo(resultData.map)
|
|
140
|
+
})
|
|
141
|
+
// 鼠标移出当前测距的点位,移除tips提示语
|
|
142
|
+
resultData.map.on('mouseout', 'measure-line-points', () => {
|
|
143
|
+
if (resultData.isMeasure) {
|
|
144
|
+
resultData.map.getCanvas().style.cursor = 'default'
|
|
145
|
+
} else {
|
|
146
|
+
resultData.map.getCanvas().style.cursor = ''
|
|
147
|
+
}
|
|
148
|
+
const deleteTips = resultData.map._container.querySelector('.delete-tips')
|
|
149
|
+
if (deleteTips) {
|
|
150
|
+
deleteTips.remove()
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
/**
|
|
154
|
+
* 点击测距点位触发
|
|
155
|
+
* @param event
|
|
156
|
+
* */
|
|
157
|
+
// eslint-disable-next-line no-undef,no-inner-declarations
|
|
158
|
+
function clickMeasurePointsHandler(event) {
|
|
159
|
+
if (resultData.jsonPoint.features.length > 2) {
|
|
160
|
+
const features = resultData.map.queryRenderedFeatures(event.point)
|
|
161
|
+
if (features.length > 0) {
|
|
162
|
+
const measureResult = resultData.map._container.querySelectorAll('.measure-line-result')
|
|
163
|
+
if (measureResult && measureResult.length > 0) {
|
|
164
|
+
Array.from(measureResult).forEach((m) => {
|
|
165
|
+
m.remove()
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
const {
|
|
169
|
+
index
|
|
170
|
+
} = features[0].properties
|
|
171
|
+
resultData.jsonPoint.features = resultData.jsonPoint.features.filter(feature => feature.properties.index !== index)
|
|
172
|
+
resultData.jsonLine.features = []
|
|
173
|
+
const featuresArr = [...resultData.jsonPoint.features]
|
|
174
|
+
const resultPoints = []
|
|
175
|
+
featuresArr.forEach((feature, index) => {
|
|
176
|
+
const nextIndex = index + 1
|
|
177
|
+
if (featuresArr[nextIndex]) {
|
|
178
|
+
const current = featuresArr[index]
|
|
179
|
+
const next = featuresArr[nextIndex]
|
|
180
|
+
resultData.jsonLine.features.push({
|
|
181
|
+
type: 'Feature',
|
|
182
|
+
geometry: {
|
|
183
|
+
type: 'LineString',
|
|
184
|
+
coordinates: [current.geometry.coordinates, next.geometry.coordinates]
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
resultPoints.push(feature.geometry.coordinates)
|
|
189
|
+
const ele = document.createElement('div')
|
|
190
|
+
ele.setAttribute('class', 'measure-line-result')
|
|
191
|
+
if (index === 0) {
|
|
192
|
+
ele.innerHTML = '起点'
|
|
193
|
+
} else {
|
|
194
|
+
// const prevIndex = index - 1
|
|
195
|
+
// const prevCoordinates = featuresArr[prevIndex].geometry.coordinates
|
|
196
|
+
const currentCoordinates = feature.geometry.coordinates
|
|
197
|
+
// const resultAngle = prevCoordinates ? getAngleHandle(prevCoordinates[0], prevCoordinates[1], currentCoordinates[0], currentCoordinates[1]).toFixed(1) + '°' : ''
|
|
198
|
+
// ele.innerHTML = `${getMetersHandler(resultPoints, currentCoordinates)} / ${resultAngle}`
|
|
199
|
+
ele.innerHTML = `${getMetersHandler(resultPoints, currentCoordinates)}`
|
|
200
|
+
}
|
|
201
|
+
if (nextIndex === featuresArr.length) { // 终点需要再加一个marker
|
|
202
|
+
// 添加关闭按钮
|
|
203
|
+
createCloseMarkerHandler(clickMeasurePointsHandler, feature.geometry.coordinates, mapObject)
|
|
204
|
+
}
|
|
205
|
+
ele.style.color = textColor
|
|
206
|
+
const left = window.document.documentElement.clientWidth > 7000 ? 20 : 8
|
|
207
|
+
const option = {
|
|
208
|
+
element: ele,
|
|
209
|
+
anchor: 'left',
|
|
210
|
+
offset: [left, 0]
|
|
211
|
+
}
|
|
212
|
+
// eslint-disable-next-line no-undef
|
|
213
|
+
new mapboxgl.Marker(option)
|
|
214
|
+
.setLngLat(feature.geometry.coordinates)
|
|
215
|
+
.addTo(resultData.map)
|
|
216
|
+
})
|
|
217
|
+
resultData.map.getSource('measure-line-points').setData(resultData.jsonPoint)
|
|
218
|
+
resultData.map.getSource('measure-line').setData(resultData.jsonLine)
|
|
219
|
+
resultData.map.getSource('measure-line-move').setData({
|
|
220
|
+
'type': 'FeatureCollection',
|
|
221
|
+
'features': []
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
closeMeasureLine(mapObject)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
resultData.clickMeasurePointsFunction = clickMeasurePointsHandler
|
|
229
|
+
resultData.map.off('click', 'measure-line-points', clickMeasurePointsHandler)
|
|
230
|
+
resultData.map.on('click', 'measure-line-points', clickMeasurePointsHandler)
|
|
231
|
+
// 添加关闭按钮
|
|
232
|
+
createCloseMarkerHandler(clickMeasurePointsHandler, coords, mapObject)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
resultData.mapDblClickFunction = dblclickHandler
|
|
236
|
+
resultData.map.off('dblclick', dblclickHandler)
|
|
237
|
+
resultData.map.on('dblclick', dblclickHandler)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 清除测距相关
|
|
242
|
+
*/
|
|
243
|
+
function clearMeasureLine(mapObject) {
|
|
244
|
+
const resultData = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
245
|
+
const measureResult = resultData.map._container.querySelectorAll('.measure-line-result')
|
|
246
|
+
if (measureResult && measureResult.length > 0) {
|
|
247
|
+
Array.from(measureResult).forEach((m) => {
|
|
248
|
+
m.remove()
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
const source = resultData.map.getSource('measure-line-points')
|
|
252
|
+
const json = {
|
|
253
|
+
'type': 'FeatureCollection',
|
|
254
|
+
'features': []
|
|
255
|
+
}
|
|
256
|
+
if (source) {
|
|
257
|
+
resultData.map.getSource('measure-line-points').setData(json)
|
|
258
|
+
resultData.map.getSource('measure-line-move').setData(json)
|
|
259
|
+
resultData.map.getSource('measure-line').setData(json)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 创建label marker
|
|
265
|
+
* @returns {*} 返回marker对象
|
|
266
|
+
*/
|
|
267
|
+
function createMeasurLineLabelMarkerHandler(mapObject) {
|
|
268
|
+
const resultData = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
269
|
+
const ele = document.createElement('div')
|
|
270
|
+
ele.style.display = 'none'
|
|
271
|
+
ele.setAttribute('class', 'measure-line-result')
|
|
272
|
+
const windowW = document.documentElement.clientWidth
|
|
273
|
+
const top = windowW > 7000 ? 120 : 44
|
|
274
|
+
const left = window.document.documentElement.clientWidth > 7000 ? 20 : 8
|
|
275
|
+
const option = {
|
|
276
|
+
element: ele,
|
|
277
|
+
anchor: 'left',
|
|
278
|
+
offset: [left, top]
|
|
279
|
+
}
|
|
280
|
+
// eslint-disable-next-line no-undef
|
|
281
|
+
const mouseLabel = new mapboxgl.Marker(option).setLngLat([0, 0]).addTo(resultData.map)
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
mouseLabel,
|
|
285
|
+
ele
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 创建测距需要的layers,points、line、measure-line-move
|
|
291
|
+
* @param jsonPoint
|
|
292
|
+
* @param jsonLine
|
|
293
|
+
* @param mapObject
|
|
294
|
+
*/
|
|
295
|
+
function createMeasureLinePLLHandler(jsonPoint, jsonLine, mapObject) {
|
|
296
|
+
const resultData = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
297
|
+
const source = resultData.map.getSource('measure-line-points')
|
|
298
|
+
if (source) {
|
|
299
|
+
resultData.map.getSource('measure-line-points').setData(jsonPoint)
|
|
300
|
+
resultData.map.getSource('measure-line-move').setData(jsonLine)
|
|
301
|
+
resultData.map.getSource('measure-line').setData(jsonLine)
|
|
302
|
+
} else {
|
|
303
|
+
resultData.map.addSource('measure-line-points', {
|
|
304
|
+
type: 'geojson',
|
|
305
|
+
data: jsonPoint
|
|
306
|
+
})
|
|
307
|
+
resultData.map.addSource('measure-line', {
|
|
308
|
+
type: 'geojson',
|
|
309
|
+
data: jsonLine
|
|
310
|
+
})
|
|
311
|
+
resultData.map.addSource('measure-line-move', {
|
|
312
|
+
type: 'geojson',
|
|
313
|
+
data: jsonLine
|
|
314
|
+
})
|
|
315
|
+
const windowW = document.documentElement.clientWidth
|
|
316
|
+
const LW = windowW > 7000 ? 6 : 2
|
|
317
|
+
const RW = windowW > 7000 ? 10.5 : 3.5
|
|
318
|
+
const CW = windowW > 7000 ? 7.5 : 2.5
|
|
319
|
+
resultData.map.addLayer({
|
|
320
|
+
id: 'measure-line-move',
|
|
321
|
+
type: 'line',
|
|
322
|
+
source: 'measure-line-move',
|
|
323
|
+
paint: {
|
|
324
|
+
'line-color': lineColor,
|
|
325
|
+
'line-width': LW,
|
|
326
|
+
'line-opacity': 0.8
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
resultData.map.addLayer({
|
|
330
|
+
id: 'measure-line',
|
|
331
|
+
type: 'line',
|
|
332
|
+
source: 'measure-line',
|
|
333
|
+
paint: {
|
|
334
|
+
'line-color': lineColor,
|
|
335
|
+
'line-width': LW,
|
|
336
|
+
'line-opacity': 1
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
resultData.map.addLayer({
|
|
340
|
+
id: 'measure-line-points',
|
|
341
|
+
type: 'circle',
|
|
342
|
+
source: 'measure-line-points',
|
|
343
|
+
paint: {
|
|
344
|
+
'circle-color': textColor,
|
|
345
|
+
'circle-radius': RW,
|
|
346
|
+
'circle-stroke-width': CW,
|
|
347
|
+
'circle-stroke-color': lineColor
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
moveLayerHandler(resultData.map)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* 将测距的三个图层移到最上层级
|
|
356
|
+
*/
|
|
357
|
+
function moveLayerHandler(mapObject) {
|
|
358
|
+
const {
|
|
359
|
+
map
|
|
360
|
+
} = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
361
|
+
const {
|
|
362
|
+
layers
|
|
363
|
+
} = map.getStyle()
|
|
364
|
+
const length = layers.length
|
|
365
|
+
const lastLayerId = map.getStyle().layers[length - 1].id
|
|
366
|
+
if (lastLayerId !== 'measure-line-points') {
|
|
367
|
+
map.moveLayer('measure-line-points', map.getStyle().layers[length - 1].id)
|
|
368
|
+
map.moveLayer('measure-line', 'measure-line-points')
|
|
369
|
+
map.moveLayer('measure-line-move', 'measure-line')
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 添加点位layer
|
|
375
|
+
* @param jsonPoint
|
|
376
|
+
* @param jsonLine
|
|
377
|
+
* @param coords
|
|
378
|
+
* @param mapObject
|
|
379
|
+
*/
|
|
380
|
+
function addMeasureLinePoint(jsonPoint, jsonLine, coords, mapObject) {
|
|
381
|
+
const {
|
|
382
|
+
map
|
|
383
|
+
} = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
384
|
+
if (jsonPoint.features.length > 0) {
|
|
385
|
+
const prev = jsonPoint.features[jsonPoint.features.length - 1]
|
|
386
|
+
jsonLine.features.push({
|
|
387
|
+
type: 'Feature',
|
|
388
|
+
geometry: {
|
|
389
|
+
type: 'LineString',
|
|
390
|
+
coordinates: [prev.geometry.coordinates, coords]
|
|
391
|
+
}
|
|
392
|
+
})
|
|
393
|
+
map.getSource('measure-line').setData(jsonLine)
|
|
394
|
+
}
|
|
395
|
+
jsonPoint.features.push({
|
|
396
|
+
type: 'Feature',
|
|
397
|
+
geometry: {
|
|
398
|
+
type: 'Point',
|
|
399
|
+
coordinates: coords
|
|
400
|
+
},
|
|
401
|
+
properties: {
|
|
402
|
+
index: jsonPoint.features.length
|
|
403
|
+
}
|
|
404
|
+
})
|
|
405
|
+
// 重新整理point点位,若是重复点位则不添加
|
|
406
|
+
const data = [...jsonPoint.features]
|
|
407
|
+
const result = []
|
|
408
|
+
data.forEach((feature, index) => {
|
|
409
|
+
if (data[index - 1]) {
|
|
410
|
+
if (data[index - 1].geometry.coordinates[0] !== feature.geometry.coordinates[0] || data[index - 1].geometry.coordinates[1] !== feature.geometry.coordinates[1]) {
|
|
411
|
+
result.push(feature)
|
|
412
|
+
}
|
|
413
|
+
} else {
|
|
414
|
+
result.push(feature)
|
|
415
|
+
}
|
|
416
|
+
})
|
|
417
|
+
jsonPoint.features = [...result]
|
|
418
|
+
map.getSource('measure-line-points').setData(jsonPoint)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* 获取两坐标点之间的距离
|
|
423
|
+
* @param points
|
|
424
|
+
* @param coords
|
|
425
|
+
* @returns {string}
|
|
426
|
+
*/
|
|
427
|
+
function getMeasureLineLength(points, coords) {
|
|
428
|
+
const _points = points.concat([coords])
|
|
429
|
+
// eslint-disable-next-line no-undef
|
|
430
|
+
const line = turf.lineString(_points)
|
|
431
|
+
// eslint-disable-next-line no-undef
|
|
432
|
+
let len = turf.length(line)
|
|
433
|
+
if (len < 1) {
|
|
434
|
+
len = Math.round(len * 1000) + '米'
|
|
435
|
+
} else {
|
|
436
|
+
len = len.toFixed(2) + '公里'
|
|
437
|
+
}
|
|
438
|
+
return len
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* 点击地图添加点位触发给当前点位绑定测距结果的labelMarker
|
|
443
|
+
* @param points
|
|
444
|
+
* @param jsonPoint
|
|
445
|
+
* @param coords
|
|
446
|
+
* @param mapObject
|
|
447
|
+
* */
|
|
448
|
+
function addMeasureLineRes(points, jsonPoint, coords, mapObject) {
|
|
449
|
+
const {
|
|
450
|
+
map
|
|
451
|
+
} = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
452
|
+
const ele = document.createElement('div')
|
|
453
|
+
ele.setAttribute('class', 'measure-line-result')
|
|
454
|
+
const left = window.document.documentElement.clientWidth > 7000 ? 20 : 8
|
|
455
|
+
const option = {
|
|
456
|
+
element: ele,
|
|
457
|
+
anchor: 'left',
|
|
458
|
+
offset: [left, 0]
|
|
459
|
+
}
|
|
460
|
+
let resultText = '起点'
|
|
461
|
+
if (points.length !== 0) {
|
|
462
|
+
// const prev = jsonPoint.features[jsonPoint.features.length - 1]
|
|
463
|
+
// const prevCoordinates = prev ? prev.geometry.coordinates : ''
|
|
464
|
+
// const resultAngle = prevCoordinates ? getAngleHandle(prevCoordinates[0], prevCoordinates[1], coords[0], coords[1]).toFixed(1) + '°' : ''
|
|
465
|
+
// resultText = `${getMeasureLineLength(points, coords)} / ${resultAngle}`
|
|
466
|
+
resultText = `${getMeasureLineLength(points, coords)}`
|
|
467
|
+
}
|
|
468
|
+
ele.innerHTML = resultText
|
|
469
|
+
|
|
470
|
+
ele.style.color = textColor
|
|
471
|
+
// eslint-disable-next-line no-undef
|
|
472
|
+
new mapboxgl.Marker(option)
|
|
473
|
+
.setLngLat(coords)
|
|
474
|
+
.addTo(map)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* 点击测距点位重新触发计算两点位之间的距离
|
|
479
|
+
* @param resultPoints
|
|
480
|
+
* @param coords
|
|
481
|
+
* @returns {string}
|
|
482
|
+
*/
|
|
483
|
+
function getMetersHandler(resultPoints, coords) {
|
|
484
|
+
if (resultPoints.length > 1) {
|
|
485
|
+
const _points = [...resultPoints]
|
|
486
|
+
// eslint-disable-next-line no-undef
|
|
487
|
+
const line = turf.lineString(_points)
|
|
488
|
+
// eslint-disable-next-line no-undef
|
|
489
|
+
let len = turf.length(line)
|
|
490
|
+
if (len < 1) {
|
|
491
|
+
len = Math.round(len * 1000) + '米'
|
|
492
|
+
} else {
|
|
493
|
+
len = len.toFixed(2) + '公里'
|
|
494
|
+
}
|
|
495
|
+
return len
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* 获取两点位之间的角度
|
|
501
|
+
* @param lng1
|
|
502
|
+
* @param lat1
|
|
503
|
+
* @param lng2
|
|
504
|
+
* @param lat2
|
|
505
|
+
* @returns {number}
|
|
506
|
+
*/
|
|
507
|
+
function getAngleHandle(lng1, lat1, lng2, lat2) {
|
|
508
|
+
const a = (90 - lat2) * Math.PI / 180
|
|
509
|
+
const b = (90 - lat1) * Math.PI / 180
|
|
510
|
+
const AOC_BOC = (lng2 - lng1) * Math.PI / 180
|
|
511
|
+
const cosc = Math.cos(a) * Math.cos(b) + Math.sin(a) * Math.sin(b) * Math.cos(AOC_BOC)
|
|
512
|
+
const sinc = Math.sqrt(1 - cosc * cosc)
|
|
513
|
+
const sinA = Math.sin(a) * Math.sin(AOC_BOC) / sinc
|
|
514
|
+
const A = Math.asin(sinA) * 180 / Math.PI
|
|
515
|
+
let res = 0
|
|
516
|
+
if (lng2 > lng1 && lat2 > lat1) {
|
|
517
|
+
res = A
|
|
518
|
+
} else if (lng2 > lng1 && lat2 < lat1) {
|
|
519
|
+
res = 180 - A
|
|
520
|
+
} else if (lng2 < lng1 && lat2 < lat1) {
|
|
521
|
+
res = 180 - A
|
|
522
|
+
} else if (lng2 < lng1 && lat2 > lat1) {
|
|
523
|
+
res = 360 + A
|
|
524
|
+
} else if (lng2 > lng1 && lat2 === lat1) {
|
|
525
|
+
res = 90
|
|
526
|
+
} else if (lng2 < lng1 && lat2 === lat1) {
|
|
527
|
+
res = 270
|
|
528
|
+
} else if (lng2 === lng1 && lat2 > lat1) {
|
|
529
|
+
res = 0
|
|
530
|
+
} else if (lng2 === lng1 && lat2 < lat1) {
|
|
531
|
+
res = 180
|
|
532
|
+
}
|
|
533
|
+
return res
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* 创建关闭按钮
|
|
538
|
+
* @param clickMeasurePointsHandler
|
|
539
|
+
* @param coords
|
|
540
|
+
* @param mapObject
|
|
541
|
+
*/
|
|
542
|
+
function createCloseMarkerHandler(clickMeasurePointsHandler, coords, mapObject) {
|
|
543
|
+
const resultData = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
544
|
+
const ele = document.createElement('div')
|
|
545
|
+
ele.setAttribute('class', 'measure-line-result close')
|
|
546
|
+
const left = window.document.documentElement.clientWidth > 7000 ? 20 : 8
|
|
547
|
+
const top = window.document.documentElement.clientWidth > 7000 ? -50 : -11
|
|
548
|
+
const option = {
|
|
549
|
+
element: ele,
|
|
550
|
+
anchor: 'bottom-left',
|
|
551
|
+
offset: [left, top]
|
|
552
|
+
}
|
|
553
|
+
ele.innerHTML = '清除'
|
|
554
|
+
ele.style.color = '#ccc'
|
|
555
|
+
// eslint-disable-next-line no-undef
|
|
556
|
+
new mapboxgl.Marker(option)
|
|
557
|
+
.setLngLat(coords)
|
|
558
|
+
.addTo(resultData.map)
|
|
559
|
+
ele.onclick = function (__e) {
|
|
560
|
+
__e.stopPropagation()
|
|
561
|
+
closeMeasureLine(resultData.map)
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* 关闭触发
|
|
567
|
+
*/
|
|
568
|
+
export function closeMeasureLine(mapObject) {
|
|
569
|
+
if (!mapObject) return
|
|
570
|
+
const resultData = getBeforeOrAfterDataByMapContainerIdHandler(mapObject)
|
|
571
|
+
if (!resultData) return
|
|
572
|
+
resultData.map.doubleClickZoom.enable()
|
|
573
|
+
clearMeasureLine(resultData.map)
|
|
574
|
+
resultData.map.off('click', resultData.mapClickFunction)
|
|
575
|
+
resultData.map.off('mousemove', resultData.mapMousemoveFunction)
|
|
576
|
+
resultData.map.off('dblclick', resultData.mapDblClickFunction)
|
|
577
|
+
resultData.map.off('click', 'measure-line-points', resultData.clickMeasurePointsFunction)
|
|
578
|
+
resultData.isMeasure = false
|
|
579
|
+
resultData.map.getCanvas().style.cursor = ''
|
|
580
|
+
resultData.map.getSource('measure-line-move').setData({
|
|
581
|
+
'type': 'FeatureCollection',
|
|
582
|
+
'features': []
|
|
583
|
+
})
|
|
584
|
+
resultData.jsonPoint = {
|
|
585
|
+
'type': 'FeatureCollection',
|
|
586
|
+
'features': []
|
|
587
|
+
}
|
|
588
|
+
resultData.jsonLine = {
|
|
589
|
+
'type': 'FeatureCollection',
|
|
590
|
+
'features': []
|
|
591
|
+
}
|
|
592
|
+
resultData.points = []
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// 根据地图对象的id来输出当前获取对应的全局变量数据
|
|
596
|
+
function getBeforeOrAfterDataByMapContainerIdHandler(mapObject) {
|
|
597
|
+
if (!mapObject) return
|
|
598
|
+
const currentMapContainerId = mapObject._container.getAttribute('id')
|
|
599
|
+
return currentMapContainerId.indexOf('-after') !== -1 ? after : before
|
|
600
|
+
}
|