forked from makinacorpus/Leaflet.MeasureControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet.measurecontrol.js
141 lines (106 loc) · 3.54 KB
/
leaflet.measurecontrol.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
L.Polyline.Measure = L.Draw.Polyline.extend({
addHooks: function() {
L.Draw.Polyline.prototype.addHooks.call(this);
if (this._map) {
this._markerGroup = new L.LayerGroup();
this._map.addLayer(this._markerGroup);
this._markers = [];
this._map.on('click', this._onClick, this);
this._startShape();
}
},
removeHooks: function () {
L.Draw.Polyline.prototype.removeHooks.call(this);
this._clearHideErrorTimeout();
//!\ Still useful when control is disabled before any drawing (refactor needed?)
this._map.off('mousemove', this._onMouseMove);
this._clearGuides();
this._container.style.cursor = '';
this._removeShape();
this._map.off('click', this._onClick);
},
_startShape: function() {
this._drawing = true;
this._poly = new L.Polyline([], this.options.shapeOptions);
this._container.style.cursor = 'crosshair';
this._updateTooltip();
this._map.on('mousemove', this._onMouseMove, this);
},
_finishShape: function () {
this._drawing = false;
this._cleanUpShape();
this._clearGuides();
this._updateTooltip();
this._map.off('mousemove', this._onMouseMove, this);
this._container.style.cursor = '';
},
_removeShape: function() {
if (!this._poly)
return;
this._map.removeLayer(this._poly);
delete this._poly;
this._markers.splice(0);
this._markerGroup.clearLayers();
},
_onClick: function(e) {
if (!this._drawing) {
this._removeShape();
this._startShape();
return;
}
L.Draw.Polyline.prototype._onClick.call(this, e);
},
_getTooltipText: function() {
var labelText = L.Draw.Polyline.prototype._getTooltipText.call(this);
if (!this._drawing) {
labelText.text = '';
}
return labelText;
}
});
L.Control.MeasureControl = L.Control.extend({
statics: {
TITLE: 'Measure distances'
},
options: {
position: 'topleft',
handler: {}
},
toggle: function() {
if (this.handler.enabled()) {
this.handler.disable.call(this.handler);
} else {
this.handler.enable.call(this.handler);
}
},
onAdd: function(map) {
var className = 'leaflet-control-draw';
this._container = L.DomUtil.create('div', 'leaflet-bar');
this.handler = new L.Polyline.Measure(map, this.options.handler);
this.handler.on('enabled', function () {
L.DomUtil.addClass(this._container, 'enabled');
}, this);
this.handler.on('disabled', function () {
L.DomUtil.removeClass(this._container, 'enabled');
}, this);
var link = L.DomUtil.create('a', className+'-measure', this._container);
link.href = '#';
link.title = L.Control.MeasureControl.TITLE;
L.DomEvent
.addListener(link, 'click', L.DomEvent.stopPropagation)
.addListener(link, 'click', L.DomEvent.preventDefault)
.addListener(link, 'click', this.toggle, this);
return this._container;
}
});
L.Map.mergeOptions({
measureControl: false
});
L.Map.addInitHook(function () {
if (this.options.measureControl) {
this.measureControl = L.Control.measureControl().addTo(this);
}
});
L.Control.measureControl = function (options) {
return new L.Control.MeasureControl(options);
};