Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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)

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
How would you normally distinguish between polygons with Leaflet? Or is Leaflet not really designed to be used like that? –  Mr_Chimp Mar 20 '12 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 Mar 20 '12 at 16:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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