forked from geodienst/lighthousemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.rangedmarker.js
39 lines (37 loc) · 1.24 KB
/
leaflet.rangedmarker.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
L.RangedMarker = L.Marker.extend({
options: {
range: 1
},
initialize: function(latlng, options) {
L.Marker.prototype.initialize.call(this, latlng, options);
const updateIconSize = this.updateIconSize.bind(this);
this.updateCallback = function() {
updateIconSize(this);
};
},
onAdd: function(map) {
L.Marker.prototype.onAdd.call(this, map);
map.on('zoomend', this.updateCallback);
this.updateIconSize(map);
},
onRemove: function(map) {
map.off('zoomend', this.updateCallback);
L.Marker.prototype.onRemove.call(this, map);
},
updateIconSize: function(map) {
let size = this._getSizeOnMap(map);
this._icon.style.width = size + 'px';
this._icon.style.height = size + 'px';
},
_getSizeOnMap: function (map) {
return this.options.range / this._getMetersPerPixel(map);
},
_getMetersPerPixel: function(map) {
var centerLatLng = map.getCenter(); // get map center
var pointC = map.latLngToContainerPoint(centerLatLng); // convert to containerpoint (pixels)
var pointX = L.point(pointC.x + 10, pointC.y); // add 10 pixels to x
// convert containerpoints to latlng's
var latLngX = map.containerPointToLatLng(pointX);
return centerLatLng.distanceTo(latLngX) / 10; // calculate distance between c and x (latitude)
}
});