forked from ubergesundheit/Leaflet.EdgeMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leaflet.EdgeMarker.js
94 lines (74 loc) · 2.43 KB
/
Leaflet.EdgeMarker.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
(function (L) {
'use strict';
L.EdgeMarker = L.Class.extend({
options: {
radius: 12,
weight: 1,
color: 'green',
fillColor: 'yellow',
fillOpacity: 1
},
initialize: function(options) {
L.setOptions(this, options);
},
addTo: function (map) {
this._map = map;
//add a method to get applicable features
L.extend(map, {
_getFeatures: function () {
var out = [];
for (var l in this._layers) {
if(typeof this._layers[l].getLatLng !== 'undefined') {
out.push(this._layers[l]);
}
}
return out;
}
});
map.on('move', this._addEdgeMarkers, this);
map.on('viewreset', this._addEdgeMarkers, this);
this._addEdgeMarkers();
map.addLayer(this);
return this;
},
onAdd: function () {},
_borderMarkerLayer: undefined,
_addEdgeMarkers: function () {
if (typeof this._borderMarkerLayer === 'undefined') {
this._borderMarkerLayer = new L.LayerGroup();
}
this._borderMarkerLayer.clearLayers();
var features = this._map._getFeatures();
for(var i = 0; i < features.length; i++) {
var currentMarkerPosition = this._map.latLngToContainerPoint(
features[i].getLatLng());
var mapPixelBounds = this._map.getSize();
if(currentMarkerPosition.y < 0 ||
currentMarkerPosition.y > mapPixelBounds.y ||
currentMarkerPosition.x > mapPixelBounds.x ||
currentMarkerPosition.x < 0) {
var y = currentMarkerPosition.y;
if( currentMarkerPosition.y < 0 ) {
y = 0;
} else if (currentMarkerPosition.y > mapPixelBounds.y) {
y = mapPixelBounds.y;
}
var x = currentMarkerPosition.x;
if (currentMarkerPosition.x > mapPixelBounds.x) {
x = mapPixelBounds.x;
} else if ( currentMarkerPosition.x < 0) {
x = 0;
}
L.circleMarker(this._map.containerPointToLatLng([x,y]), this.options)
.addTo(this._borderMarkerLayer);
}
}
if(!this._map.hasLayer(this._borderMarkerLayer)) {
this._borderMarkerLayer.addTo(this._map);
}
}
});
L.edgeMarker = function (options) {
return new L.EdgeMarker(options);
};
})(L);