Combining geojson and json for leaftlet

落爺英雄遲暮 提交于 2019-12-08 05:35:26

When Leaflet parses your GeoJSON data and builds a GeoJSON Layer Group (that you have stored in your geojsonLayer variable) out of it, it records the features data into the featureproperty of each corresponding layer.

So for instance, in your geojsonLayer you will get (among others) a polygon with: (below referred to as "layer")

layer.feature.type // "Feature"
layer.feature.geometry // {"type":"Polygon","coordinates":[[[-73,-7],[-73,-8]]]}
layer.feature.properties // {"field1":"value1","field2":"value2","ID":"1"}

So for example you could do:

geojsonLayer.eachLayer(function (layer) {
  if (layer.feature.properties.ID === jsonObj.id) {
    for (var key in jsonObj) {
      layer.feature.properties[key] = jsonObj[key];
    }
  }
});

Of course you could then improve your algorithm to cache the references to your Leaflet layers, instead of having to loop through geojsonLayer every time.

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