-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.measure.js
316 lines (274 loc) · 8.4 KB
/
leaflet.measure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
234
235
236
237
238
239
240
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
L.Control.Measure = L.Control.extend({
options: {
position: 'topleft',
// weather to use keyboard control for this plugin
keyboard: true,
// shortcut to activate measure
activeKeyCode: 'M'.charCodeAt(0),
// shortcut to cancel measure, defaults to 'Esc'
cancelKeyCode: 27,
// line color
lineColor: 'black',
// line weight
lineWeight: 2,
// line dash
lineDashArray: '6, 6',
// line opacity
lineOpacity: 1,
// format distance method
formatDistance: null,
// define text color
textColor: 'black'
},
initialize: function (options) {
// apply options to instance
L.Util.setOptions(this, options)
},
onAdd: function (map) {
var className = 'leaflet-control-zoom leaflet-bar leaflet-control'
var container = L.DomUtil.create('div', className)
this._createButton('⇢', 'Measure',
'leaflet-control-measure leaflet-bar-part leaflet-bar-part-top-and-bottom',
container, this._toggleMeasure, this)
if (this.options.keyboard) {
L.DomEvent.on(document, 'keydown', this._onKeyDown, this)
}
return container
},
onRemove: function (map) {
if (this.options.keyboard) {
L.DomEvent.off(document, 'keydown', this._onKeyDown, this)
}
},
_createButton: function (html, title, className, container, fn, context) {
var link = L.DomUtil.create('a', className, container)
link.innerHTML = html
link.href = '#'
link.title = title
L.DomEvent
.on(link, 'click', L.DomEvent.stopPropagation)
.on(link, 'click', L.DomEvent.preventDefault)
.on(link, 'click', fn, context)
.on(link, 'dbclick', L.DomEvent.stopPropagation)
return link
},
_toggleMeasure: function () {
this._measuring = !this._measuring
if (this._measuring) {
L.DomUtil.addClass(this._container, 'leaflet-control-measure-on')
this._startMeasuring()
} else {
L.DomUtil.removeClass(this._container, 'leaflet-control-measure-on')
this._stopMeasuring()
}
},
_startMeasuring: function () {
this._oldCursor = this._map._container.style.cursor
this._map._container.style.cursor = 'crosshair'
this._doubleClickZoom = this._map.doubleClickZoom.enabled()
this._map.doubleClickZoom.disable()
this._isRestarted = false
L.DomEvent
.on(this._map, 'mousemove', this._mouseMove, this)
.on(this._map, 'click', this._mouseClick, this)
.on(this._map, 'dbclick', this._finishPath, this)
if (!this._layerPaint) {
this._layerPaint = L.layerGroup().addTo(this._map)
}
if (!this._points) {
this._points = []
}
},
_stopMeasuring: function () {
this._map._container.style.cursor = this._oldCursor
L.DomEvent
.off(this._map, 'mousemove', this._mouseMove, this)
.off(this._map, 'click', this._mouseClick, this)
.off(this._map, 'dbclick', this._finishPath, this)
if (this._doubleClickZoom) {
this._map.doubleClickZoom.enabled()
}
if (this._layerPaint) {
this._layerPaint.clearLayers()
}
this._restartPath()
},
_mouseMove: function (e) {
if (!e.latlng || !this._lastPoint) {
return
}
if (!this._layerPaintPathTemp) {
// customize style
this._layerPaintPathTemp = L.polyline([this._lastPoint, e.latlng], {
color: this.options.lineColor,
weight: this.options.lineWeight,
opacity: this.options.lineOpacity,
clickable: false,
dashArray: this.options.lineDashArray,
interactive: false
}).addTo(this._layerPaint)
} else {
// replace the current layer to the newest draw points
this._layerPaintPathTemp.getLatLngs().splice(0, 2, this._lastPoint, e.latlng)
// force path layer update
this._layerPaintPathTemp.redraw()
}
// tooltip
if (this._tooltip) {
if (!this._distance) {
this._distance = 0
}
this._updateTooltipPosition(e.latlng)
var distance = e.latlng.distanceTo(this._lastPoint)
this._updateTooltipDistance(this._distance + distance, distance)
}
},
_mouseClick: function (e) {
if (!e.latlng) {
return
}
if (this._isRestarted) {
this._isRestarted = false
return
}
if (this._lastPoint && this._tooltip) {
if (!this._distance) {
this._distance = 0
}
this._updateTooltipPosition(e.latlng)
var distance = e.latlng.distanceTo(this._lastPoint)
this._updateTooltipDistance(this._distance + distance, distance)
this._distance += distance
}
this._createTooltip(e.latlng)
// main layer add to layerPaint
if (this._lastPoint && !this._layerPaintPath) {
this._layerPaintPath = L.polyline([this._lastPoint], {
color: this.options.lineColor,
weight: this.options.lineWeight,
opacity: this.options.lineOpacity,
clickable: false,
interactive: false
}).addTo(this._layerPaint)
}
// push current point to the main layer
if (this._layerPaintPath) {
this._layerPaintPath.addLatLng(e.latlng)
}
if (this._lastPoint) {
if (this._lastCircle) {
this._lastCircle.off('click', this._finishPath, this)
}
this._lastCircle = this._createCircle(e.latlng).addTo(this._layerPaint)
this._lastCircle.on('click', this._finishPath, this)
}
this._lastPoint = e.latlng
},
_finishPath: function (e) {
if (e) {
L.DomEvent.preventDefault(e)
}
if (this._lastCircle) {
this._lastCircle.off('click', this._finishPath, this)
}
if (this._tooltip) {
// when remove from map, the _icon property becomes null
this._layerPaint.removeLayer(this._tooltip)
}
if (this._layerPaint && this._layerPaintPathTemp) {
this._layerPaint.removeLayer(this._layerPaintPathTemp)
}
// clear everything
this._restartPath()
},
_restartPath: function () {
this._distance = 0
this._lastCircle = undefined
this._lastPoint = undefined
this._tooltip = undefined
this._layerPaintPath = undefined
this._layerPaintPathTemp = undefined
// flag to stop propagation events...
this._isRestarted = true
},
_createCircle: function (latlng) {
return new L.CircleMarker(latlng, {
color: 'black',
opacity: 1,
weight: 1,
fillColor: 'white',
fill: true,
fillOpacity: 1,
radius: 4,
clickable: Boolean(this._lastCircle)
})
},
_createTooltip: function (position) {
var icon = L.divIcon({
className: 'leaflet-measure-tooltip',
iconAnchor: [-5, -5]
})
this._tooltip = L.marker(position, {
icon: icon,
clickable: false
}).addTo(this._layerPaint)
},
_updateTooltipPosition: function (position) {
this._tooltip.setLatLng(position)
},
_updateTooltipDistance: function (total, difference) {
if (!this._tooltip._icon) {
return
}
var totalRound = this._formatDistance(total)
var differenceRound = this._formatDistance(difference)
var text = '<div class="leaflet-measure-tooltip-total" style="color:'+ this.options.textColor +'">' + totalRound + '</div>'
if (differenceRound > 0 && totalRound !== differenceRound) {
text += '<div class="leaflet-measure-tooltip-difference" style="color:'+ this.options.textColor +'">(+' + differenceRound + ')</div>'
}
this._tooltip._icon.innerHTML = text
},
_formatDistance: function (val) {
if (typeof this.options.formatDistance === 'function') {
return this.options.formatDistance(val);
}
if (val < 1000) {
return Math.round(val) + 'm'
} else {
return Math.round((val / 1000) * 100) / 100 + 'km'
}
},
_onKeyDown: function (e) {
// key control
switch (e.keyCode) {
case this.options.activeKeyCode:
if (!this._measuring) {
this._toggleMeasure()
}
break
case this.options.cancelKeyCode:
// if measuring, cancel measuring
if (this._measuring) {
if (!this._lastPoint) {
this._toggleMeasure()
} else {
this._finishPath()
this._isRestarted = false
}
}
break
}
}
})
L.control.measure = function (options) {
return new L.Control.Measure(options)
}
L.Map.mergeOptions({
measureControl: false
})
L.Map.addInitHook(function () {
if (this.options.measureControl) {
this.measureControl = new L.Control.Measure()
this.addControl(this.measureControl)
}
})