且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用onEachFeature和geojson将标记添加到传单中的不同图层

更新时间:2023-01-02 07:47:06

我相信您已经快到了.在onEachFeature函数中,您可以简单地将图层相应地添加到不同的图层组中.然后,您可以随时将图层组添加到地图.试试看:

I believe you are nearly there. In your onEachFeature function, you can simply add layers to your different Layer Groups accordingly. Then, you can add the Layer Groups to the map whenever you want. Try this out:

function onEachFeature(feature,layer) {
    if(feature.properties.name == "red") {
        // add only 'red' markers to Layer Group
        redLayer.addLayer(layer);
    } else {
        // add only 'blue' markers to Layer Group (assuming just red/blue markers)
        blueLayer.addLayer(layer);
    }
}

现在在代码中的其他位置,您可以将以下图层添加到地图中:

Now somewhere else in your code you can add these layers to your map:

redLayer.addTo(mapid);
blueLayer.addTo(mapid);

除非您希望最初在地图上包含所有标记,否则请确保在L.geoJSON()之后删除.addTo()方法.希望这会有所帮助!

Just make sure to remove the .addTo() method after the L.geoJSON() unless you want all the markers on the map initially. Hope this helps!