-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathleaflet-arrows.js
294 lines (244 loc) · 9.48 KB
/
leaflet-arrows.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
// Leaflet Arrows by Meteotest
// https://github.com/meteotest/leaflet-arrows
// Module Loader Boilerplate
(function(factory, window) {
// define an AMD module that relies on 'leaflet'
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
// define a Common JS module that relies on 'leaflet'
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
}
// attach your plugin to the global 'L' variable
if (typeof window !== 'undefined' && window.L) {
window.L.Arrow = factory(L);
}
}(function(L) {
// beware! the arrow factory
var Arrow = L.FeatureGroup.extend({
options: {
distanceUnit: 'km', // can be [px,km]
stretchFactor: 1, // should the distance be stretched?
arrowheadLength: 4, // in distance unit
arrowheadClosingLine: false, // should a closing third line be drawn?
arrowheadDegree: 155, // degree of arrowhead
clickableWidth: 10, // defines the width in pixels of the "phantom" path to capture click events on a line
//optional: popupContent: function(data) {},
// add your own custom validator, probably not needed...
// the validator gets the bound data object as argument
validator: function(data) {
return typeof data.latlng !== "undefined" &&
typeof data.distance !== "undefined" && !isNaN(data.distance);
}, // validator is a callback function that takes the data object of the current point and returns whether it is 'valid'. Invalid arrows will be drawn gray
colorScheme: function() {
return this.color;
}, // add own colorscheme callback
circleRadiusInvalidPoint: 1000, // Radius of the circle to display missing or '0'-value
// path options
color: '#333',
opacity: 0.9, // stroke opacity
fillOpacity: 0.9,
weight: 4, // the width of the arrow
smoothFactor: 0,
radius: 5, // default radius, when distance is 0
defaultPointOptions: {
stroke: false,
fillOpacity: 0.8,
fillColor: '#111',
radius: 7
},
invalidPointOptions: {
stroke: false,
fillOpacity: 0.8,
fillColor: 'red',
radius: 7
},
drawSourceMarker: false,
sourceMarkerOptions: {
stroke: false,
fillOpacity: 1,
fillColor: '#333',
radius: 5
}
},
/**
* The data opject needs at least following properties:
* - latlng : L.LatLng
* - degree : float
* - distance: float
*/
initialize: function(data, options) {
L.Util.setOptions(this, options);
this.setData(data);
},
onAdd: function(map) {
this._map = map;
this.redraw();
// add a viewreset event listener for updating layer's position
map.on('viewreset', this._reset, this);
},
onRemove: function() {
// remove layer's DOM elements and listeners
for (var i in this._layers) {
if (this._layers.hasOwnProperty(i)) {
this._map.removeLayer(this._layers[i]);
}
}
if (typeof this._sourceMarker !== "undefined") {
this._map.removeLayer(this._sourceMarker);
}
this._map.off('viewreset', this._reset, this);
},
// draw
redraw: function() {
// only draw when on map
if (!this._map) {
return;
}
this._layers = {};
// bind popup to the whole feature group
if (typeof this.options.popupContent === 'function') {
this.bindPopup(this.options.popupContent(this._data));
}
if (this._data.valid && typeof this.options.colorScheme === "function") {
this.options.color = this.options.colorScheme(this._data);
}
this._data.distance = parseFloat(this._data.distance);
// if distance or degree is falsy/Zero then draw a point instead of an arrow
if (!this._data.valid || !this._data.distance || !this._data.angle) {
var circle;
var pathOptions = this._data.valid ? this.options.defaultPointOptions : this.options.invalidPointOptions;
if (this.options.distanceUnit.toLowerCase() === 'km') {
// use a tenth of the supplied radius (usally used in the circlemarker)
circle = L.circle(this._data.latlng, this.options.radius / 10, pathOptions);
} else {
circle = L.circleMarker(this._data.latlng, pathOptions);
}
this.addLayer(circle);
} else {
var theLine = [
this._data.latlng,
this._calculateEndPoint(
this._data.latlng,
this._data.distance, this._data.angle
)
];
var theArrow = this._calculateArrowArray(theLine[1]);
var backgroundPathOption = L.Util.setOptions({}, this.pathOptions);
backgroundPathOption.opacity = 0;
backgroundPathOption.weight = this.options.clickableWidth;
var invisibleBackgroundPolyline = new L.Polyline([theLine, theArrow],
backgroundPathOption);
var polyline = new L.Polyline([theLine, theArrow], this.options);
this.addLayer(invisibleBackgroundPolyline);
this.addLayer(polyline);
// that special case, where a circle has to be drawn on the source of the arrow
if (this.options.drawSourceMarker) {
if (typeof this._sourceMarker === 'undefined') {
// use the same coloar as the arrow does
this.options.sourceMarkerOptions.fillColor = this.color;
this._sourceMarker = L.circleMarker(this._data.latlng, this.options.sourceMarkerOptions);
} else {
// there is a chance, that the latlng values have been changed by the setData-function
this._sourceMarker.setLatLng(this._data.latlng);
}
}
}
},
// custom methods
// just change the angle of the arrow
setAngle: function(angle) {
if (isNaN(angle)) {
this._data.angle = 0; // undefined direction.
} else {
if (this.options.isWindDegree) {
this._data.angle = angle - 180;
if (this.options.distanceUnit.toLowerCase() === 'px') {
this._data.angle -= 90;
}
} else {
this._data.angle = angle;
}
}
this.redraw();
},
// just change the length of the arrow
setDistance: function(distance) {
if (isNaN(distance)) {
this._data.distance = 0;
} else {
this._data.distance = parseFloat(distance);
}
this.redraw();
},
// use this method to update the whole dataset that corresponds to the arrow
setData: function(data) {
this._data = data;
if (this.options.validator(this._data)) {
this._data.valid = true;
this.setAngle(data.degree);
} else {
this._data.valid = false;
this._data.distance = NaN;
this._data.degree = NaN;
}
},
// private methods
// When using Screen Pixels as unit, the arrows have to be
// recalculated after the zoom level has changed
_reset: function() {
if (this.options.distanceUnit.toLowerCase() === 'px') {
this.redraw();
}
},
_calculateEndPoint: function(latlng, dist, degree) {
/*
* http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5115/Geographic-Distance-and-Azimuth-Calculations.htm
*/
// don't use this._data here.
// this function is also used to find the points of the arrow
var distance = dist * this.options.stretchFactor,
d2r = (Math.PI / 180), // degree 2 radius
r2d = (180 / Math.PI);
if (this.options.distanceUnit.toLowerCase() === 'km') {
var R = 6378.137, // earth radius in kmeters
bearing = degree * d2r;
distance = distance / R;
var a = Math.acos(Math.cos(distance) *
Math.cos((90 - latlng.lat) * d2r) +
Math.sin((90 - latlng.lat) * d2r) *
Math.sin(distance) *
Math.cos(bearing));
var B = Math.asin(Math.sin(distance) * Math.sin(bearing) / Math.sin(a));
return new L.LatLng(90 - a * r2d, B * r2d + latlng.lng);
} else if (this.options.distanceUnit.toLowerCase() === 'px') {
var sourcePoint = this._map.latLngToLayerPoint(latlng);
var rad = degree * d2r;
var vector = L.point(Math.cos(rad) * distance, Math.sin(rad) * distance);
var targetPoint = sourcePoint.add(vector);
return this._map.layerPointToLatLng(targetPoint);
} else {
throw Error("calculate end point undefined for distanceUnit: " + this.options.distanceUnit);
}
},
_calculateArrowArray: function(latlng) {
// calculates the Array for the arrow
// latlng is the position, where the arrow is added
var degree = this._data.angle;
if (latlng.length !== undefined) {
latlng = new L.LatLng(latlng);
}
var firstEdge = this._calculateEndPoint(latlng,
this.options.arrowheadLength,
degree - this.options.arrowheadDegree);
var arr = [firstEdge, latlng,
this._calculateEndPoint(latlng, this.options.arrowheadLength, degree + this.options.arrowheadDegree)
];
if (this.options.arrowheadClosingLine) {
arr.push(firstEdge);
}
return arr;
}
});
return Arrow;
}, window));