I have a Leaflet map with a Cloudmade base layer and a single layer of polygons. Each of these polygons has a reference number attached as a property.

I would like to select a reference number from a list (or whatever) and then pick out the object by its reference number and zoom to the extents of that object.

How can I pick an object by its properties? (Presumably once I have done that zooming to extents shouldn't be too hard)

1 Answer 1


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) {

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  ;)

  • How would you normally distinguish between polygons with Leaflet? Or is Leaflet not really designed to be used like that?
    – Mr_Chimp
    Commented Mar 20, 2012 at 15:04
  • @Mr_Chimp Well, when you create a polygon, you get a reference to it. From my understanding of the doc, Leaflet does not manage features beyond the “view” part. I must say I totally agree with the idea: I prefer my mapping library to stay there, and handle my business logic on my own. It could be that the undocumented ILayer or IFeature bring something someday but for the moment, as said in my answer, the needed elements (iteration, contained features…) are private.
    – MattiSG
    Commented Mar 20, 2012 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.