-
Notifications
You must be signed in to change notification settings - Fork 15
/
leaflet-distance-marker.js
173 lines (153 loc) · 5.45 KB
/
leaflet-distance-marker.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
/*
* https://github.com/adoroszlai/leaflet-distance-markers
*
* The MIT License (MIT)
*
* Copyright (c) 2014- Doroszlai Attila, 2016- Phil Whitehurst
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
L.DistanceMarkers = L.LayerGroup.extend({
initialize: function (line, map, options) {
options = options || {};
var offset = options.offset || 1000;
var showAll = Math.min(map.getMaxZoom(), options.showAll || 12);
var cssClass = options.cssClass || 'dist-marker';
var iconSize = options.iconSize !== undefined ? options.iconSize : [12, 12];
var textFunction = options.textFunction || function(distance, i, offset) {
return i;
};
var zoomLayers = {};
// Get line coords as an array
var coords = line;
if (typeof line.getLatLngs == 'function') {
coords = line.getLatLngs();
}
// Get accumulated line lengths as well as overall length
var accumulated = L.GeometryUtil.accumulatedLengths(line);
var length = accumulated.length > 0 ? accumulated[accumulated.length - 1] : 0;
// Position in accumulated line length array
var j = 0;
// Number of distance markers to be added
var count = Math.floor(length / offset);
for (var i = 1; i <= count; ++i) {
var distance = offset * i;
// Find the first accumulated distance that is greater
// than the distance of this marker
while (j < accumulated.length - 1 && accumulated[j] < distance) {
++j;
}
// Now grab the two nearest points either side of
// distance marker position and create a simple line to
// interpolate on
var p1 = coords[j - 1];
var p2 = coords[j];
var m_line = L.polyline([p1, p2]);
var ratio = (distance - accumulated[j - 1]) / (accumulated[j] - accumulated[j - 1]);
var position = L.GeometryUtil.interpolateOnLine(map, m_line, ratio);
var text = textFunction.call(this, distance, i, offset);
var icon = L.divIcon({ className: cssClass, html: text, iconSize: iconSize });
var marker = L.marker(position.latLng, { title: text, icon: icon });
// visible only starting at a specific zoom level
var zoom = this._minimumZoomLevelForItem(i, showAll);
if (zoomLayers[zoom] === undefined) {
zoomLayers[zoom] = L.layerGroup();
}
zoomLayers[zoom].addLayer(marker);
}
var currentZoomLevel = 0;
var markerLayer = this;
var updateMarkerVisibility = function() {
var oldZoom = currentZoomLevel;
var newZoom = currentZoomLevel = map.getZoom();
if (newZoom > oldZoom) {
for (var i = oldZoom + 1; i <= newZoom; ++i) {
if (zoomLayers[i] !== undefined) {
markerLayer.addLayer(zoomLayers[i]);
}
}
} else if (newZoom < oldZoom) {
for (var i = oldZoom; i > newZoom; --i) {
if (zoomLayers[i] !== undefined) {
markerLayer.removeLayer(zoomLayers[i]);
}
}
}
};
map.on('zoomend', updateMarkerVisibility);
this._layers = {}; // need to initialize before adding markers to this LayerGroup
updateMarkerVisibility();
},
_minimumZoomLevelForItem: function (item, showAllLevel) {
var zoom = showAllLevel;
var i = item;
while (i > 0 && i % 2 === 0) {
--zoom;
i = Math.floor(i / 2);
}
return zoom;
},
});
L.Polyline.include({
_originalOnAdd: L.Polyline.prototype.onAdd,
_originalOnRemove: L.Polyline.prototype.onRemove,
_originalSetLatLngs: L.Polyline.prototype.setLatLngs,
addDistanceMarkers: function () {
if (this._map && this._distanceMarkers) {
this._map.addLayer(this._distanceMarkers);
}
},
removeDistanceMarkers: function () {
if (this._map && this._distanceMarkers) {
this._map.removeLayer(this._distanceMarkers);
}
},
onAdd: function (map) {
this._originalOnAdd(map);
this.createDistanceMarkers();
},
createDistanceMarkers: function() {
if (!this._map) return;
var opts = this.options.distanceMarkers || {};
if (this._distanceMarkers === undefined && this.options.distanceMarkers) {
this._distanceMarkers = new L.DistanceMarkers(this, this._map, opts);
}
if (opts.lazy === undefined || opts.lazy === false) {
this.addDistanceMarkers();
}
},
destroyDistanceMarkers: function() {
if (this._distanceMarkers) {
this._distanceMarkers = undefined;
}
},
setLatLngs: function (latlngs) {
var recreate = this._map && this._distanceMarkers;
this.removeDistanceMarkers();
this.destroyDistanceMarkers();
var result = this._originalSetLatLngs(latlngs);
if (recreate) {
this.createDistanceMarkers();
}
return result;
},
onRemove: function (map) {
this.removeDistanceMarkers();
this._originalOnRemove(map);
}
});