I don't know of any way Leaflet would allow direct property-based lookup.
Unfortunately, ILayer
(the layer interface) does not, nor its inheriting LayerGroup
, allow access to their private _layer
property, so you will have to implement your own indexing.
Use something like a polygonIndex
hash that is filled when adding your polygons:
polygonIndex[currentIndex] = new Polygon(…);
Then use Object.keys(polygonIndex)
to generate your list, and bind the selection to something like:
function selectPolygon(wantedIndex) {
map.fitBounds(polygonIndex[wantedIndex].getBounds());
}
This method shouldn't have any significant memory impact, considering the size of a mapping library and the fact that each referenced element also lives on the map.
However, if you're sure your reference numbers will be pure integers and continuous, you could improve it by using an array instead of a hash. But this is on the verge of premature optimization ;)