7

I want to interact with a leaflet powered map's GeoJson overlay (polygons) from outside of L.'s realm, but I don't seem to be able to access objects created by L..

Interaction would include:

  • getBounds(myFeature)
  • fitBounds(myFeature)
  • setStyle etc

I can see Leaflet exposing L.GeoJSON.getFeature(), but I don't seem to be able to squeeze anything out of it. No documentation, and the inspector seems to suggest it does not take arguments... :\

Is this just there for future development?

enter image description here

datafunk
  • 636
  • 9
  • 18

1 Answers1

14

You may use getLayer to get the feature by its id.
http://leafletjs.com/reference.html#layergroup-getlayer

var geojsonLayer = L.geoJson(data,{
    onEachFeature: function(feature, layer) {
        layer._leaflet_id = feature.id;                                    
    }});
geojsonLayer.addTo(map);

feature = geojsonLayer.getLayer(12345); //your feature id here
alert(feature.feature.id);
asir6
  • 584
  • 6
  • 14
  • It seems like the logical place to go / look, but I cannot get it info out. Do you happen to have an example I could look at? – datafunk Feb 20 '15 at 01:08
  • Sorry, been completly hijacked with another project.I am yet to hook in some of functions, but I can now access Leaflet's objects from external input. Thanks for your help. – datafunk Feb 24 '15 at 00:14