Accessing Leaflet.js GeoJson features from outside

一曲冷凌霜 提交于 2019-12-05 01:38:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/28618049/accessing-leaflet-js-geojson-features-from-outside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!