Created
February 26, 2014 02:48
-
-
Save patrickarlt/9222579 to your computer and use it in GitHub Desktop.
Robust unique ids in Leaflet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyMarkerGroup = L.FeatureGroup.extend({ | |
// override the 'getLayerId' function to return your ids rather then leaflets internal ids | |
getLayerId: function(layer){ | |
return layer.id; | |
} | |
getLayersById: function(arrayOfLayerIds){ | |
var layers = []; | |
// some logic to loop over layers and select them by id | |
// leaflet maintains layers (markers) in an internal _layers property | |
for (var i = arrayOfLayerIds.length - 1; i >= 0; i--) { | |
var id = arrayOfLayerIds[i]; | |
layers.push(this._layers[id]); | |
} | |
return layers; | |
} | |
}); | |
var markers = new MyMarkerGroup(); | |
var marker = new L.marker(latlng, options); | |
marker.id = "something"; // assign your id | |
markers.addLayer(marker); // add your layer | |
var aMarker = markers.getLayer("something"); // gets marker by id | |
var someMarkers = markers.getLayersById(["foo", "bar"]); // get markers by their ids |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment