Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make EdgeMarker track a single coronate or marker #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 62 additions & 56 deletions Leaflet.EdgeMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
options: {
distanceOpacity: false,
distanceOpacityFactor: 4,
layerGroup: null,
rotateIcons: true,
findEdge : function (map){
return L.bounds([0,0], map.getSize());
Expand All @@ -22,28 +21,14 @@
})
},

initialize: function(options) {
initialize: function(target,options) {
this._target = target;
L.setOptions(this, options);
},

addTo: function(map) {
this._map = map;

// add a method to get applicable features
if (typeof map._getFeatures !== 'function') {
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);

Expand All @@ -55,49 +40,42 @@
},

destroy: function() {
if (this._map && this._borderMarkerLayer) {
if (this._map ) {
this._map.off('move', this._addEdgeMarkers, this);
this._map.off('viewreset', this._addEdgeMarkers, this);
this._removeMarker();
this._map.removeLayer(this);
}
},

this._borderMarkerLayer.clearLayers();
this._map.removeLayer(this._borderMarkerLayer);

delete this._map._getFeatures;
setTarget: function (latlng){
this._target=latlng;
this._addEdgeMarkers();
},

this._borderMarkerLayer = undefined;
}
_makeThisTarget: function (object)
{
this.setTarget(object.latlng);
},

onClick: function(e) {
this._map.setView(e.target.options.latlng, this._map.getZoom());
this._map.setView(e.latlng, this._map.getZoom());
},

onAdd: function() {},

_borderMarkerLayer: undefined,
_marker: undefined,
_target: undefined,

_addEdgeMarkers: function() {
if (typeof this._borderMarkerLayer === 'undefined') {
this._borderMarkerLayer = new L.LayerGroup();
}
this._borderMarkerLayer.clearLayers();

var features = [];
if (this.options.layerGroup != null) {
features = this.options.layerGroup.getLayers();
} else {
features = this._map._getFeatures();
}

var mapPixelBounds = this.options.findEdge(this._map);
this._removeMarker();
if ( this._target != undefined){
var mapPixelBounds = this.options.findEdge(this._map);

var markerWidth = this.options.icon.options.iconSize[0];
var markerHeight = this.options.icon.options.iconSize[1];
var markerWidth = this.options.icon.options.iconSize[0];
var markerHeight = this.options.icon.options.iconSize[1];

for (var i = 0; i < features.length; i++) {
var currentMarkerPosition = this._map.latLngToContainerPoint(
features[i].getLatLng()
);
var currentMarkerPosition = this._map.latLngToContainerPoint( this._target);

if (currentMarkerPosition.y < mapPixelBounds.min.y ||
currentMarkerPosition.y > mapPixelBounds.max.y ||
Expand Down Expand Up @@ -143,23 +121,51 @@
newOptions.angle = angle;
}

var ref = { latlng: features[i].getLatLng() };
var ref = { latlng: this._targert };
newOptions = L.extend({}, newOptions, ref);

var marker = L.rotatedMarker(
this._map.containerPointToLatLng([x, y]),
newOptions
).addTo(this._borderMarkerLayer);

marker.on('click', this.onClick, marker);
this._marker = L.rotatedMarker(
this._map.containerPointToLatLng([x, y]),
newOptions
).addTo(this._map);
}
}
if (!this._map.hasLayer(this._borderMarkerLayer)) {
this._borderMarkerLayer.addTo(this._map);
},

_removeMarker: function (){
if (! (typeof this._marker === 'undefined')) {
this._map.removeLayer(this._marker);
this._marker=undefined;
}
}
});

L[classToExtend].include({

bindEdgeMarker: function (options){

this._edgeMarker = L.edgeMarker(this.getLatLng(),options);
if (!this._edgeMarkerHandlersAdded) {
this._edgeMarker.addTo(this._map);
this.on('remove', this._edgeMarker.destroy, this._edgeMarker); // does not fire on leaflet 0.7
this.on('move', this._edgeMarker._makeThisTarget, this._edgeMarker);
this._edgeMarkerHandlersAdded = true;
}
return this;
},

unbindEdgeMarker: function (){
if (this._edgeMarker){
this.off('remove', this._edgeMarker.remove, this._edgeMarker);// does not have effect on leaflet 0.7
this.off('move', this._edgeMarker._makeThisTarget, this._edgeMarker);
this._edgeMarker.remove();
this._edgeMarker=undefined;
this._edgeMarkerHandlersAdded=false;
}
return this;
},
});

/*
* L.rotatedMarker class is taken from https://github.com/bbecquet/Leaflet.PolylineDecorator.
*/
Expand Down Expand Up @@ -218,7 +224,7 @@
return new L.RotatedMarker(pos, options);
};

L.edgeMarker = function(options) {
return new L.EdgeMarker(options);
L.edgeMarker = function(target, options) {
return new L.EdgeMarker(target, options);
};
})(L);
41 changes: 21 additions & 20 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ <h3>Leaflet 1.0 Demo</h3>
<p><a href="index-1.0.html"><b>Works in Leaflet 0.7.x and 1.0</b> (Demo)</a></p>
<a href="https://github.com/ubergesundheit/Leaflet.EdgeMarker"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a>
<div>
<div style="opacity: 0.80; background-color: #fff; z-index: 10000; height:100%;width:200px; position:absolute" >
menu
<div style="opacity: 0.80; background-color: #fff; z-index: 10000; height:100%;width:200px; position:absolute" >
menu
</div>
<div id="map"></div>
</div>
Expand All @@ -79,19 +79,17 @@ <h2>Usage</h2>
L.marker([52.52,13.40]).addTo(map);

// add the EdgeMarker to the map.
L.edgeMarker({
icon: L.icon({ // style markers
iconUrl: 'images/edge-arrow-marker-black.png',
var icon = L.icon({ // style markers
iconUrl : 'images/edge-arrow-marker.png',
clickable: true,
iconSize: [48, 48],
iconSize: [48,48],
iconAnchor: [24, 24]
},
rotateIcons: true, // rotate EdgeMarkers depending on their relative position
layerGroup: null // you can specify a certain L.layerGroup to create the edge markers from.
}).addTo(map);

// if you want to remove the edge markers
// edgeMarkerLayer.destroy();
})
for (var l in map._layers) {
if (typeof map._layers[l].getLatLng !== 'undefined') {
map._layers[l].bindEdgeMarker({icon:icon});
}
}
</code>
</pre>

Expand Down Expand Up @@ -145,19 +143,22 @@ <h2>Usage</h2>
L.marker([46.95,7.45]).addTo(map);
L.marker([50.43,30.52]).addTo(map);

// add the EdgeMarker to the map.
var edgeMarkerLayer = L.edgeMarker({
findEdge : function(map){return L.bounds([200,0],map.getSize());},
icon: L.icon({ // style markers
// add the EdgeMarker to the map. -->
var options = {
findEdge : function(map){return L.bounds([200,0],map.getSize());},
icon: L.icon({ // style markers
iconUrl : 'images/edge-arrow-marker.png',
clickable: true,
iconSize: [48,48],
iconAnchor: [24, 24]
})
}).addTo(map);
};
for (var l in map._layers) {
if (typeof map._layers[l].getLatLng !== 'undefined') {
map._layers[l].bindEdgeMarker(options);
}
}

// if you want to remove the edge markers
// edgeMarkerLayer.destroy();
</script>

</body>
Expand Down