Javascript 如何在两个图层组中添加两个geoJSON要素集合
我有两个geoJSON功能集合需要添加到地图中,我还希望通过图层可见性控制器打开和关闭它们,如中所示Javascript 如何在两个图层组中添加两个geoJSON要素集合,javascript,leaflet,openstreetmap,Javascript,Leaflet,Openstreetmap,我有两个geoJSON功能集合需要添加到地图中,我还希望通过图层可见性控制器打开和关闭它们,如中所示 如何执行此操作?由于在加载GeoJSON时创建了一个层,因此可以将其作为覆盖层添加到层控件中(只需修改该示例并替换cities层。关于传单的GeoJSON层L.GeoJSON的使用也有一个很好的教程,可以在这里找到:L.GeoJSON:您已经找到了关于L.control.layers的教程,这里是它的参考: 这实际上非常简单,只需创建一个layercontrol,使用您最喜欢的XHR库将GeoJ
如何执行此操作?由于在加载GeoJSON时创建了一个层,因此可以将其作为覆盖层添加到层控件中(只需修改该示例并替换cities层。关于传单的GeoJSON层
L.GeoJSON
的使用也有一个很好的教程,可以在这里找到:L.GeoJSON
:您已经找到了关于L.control.layers
的教程,这里是它的参考:
这实际上非常简单,只需创建一个layercontrol,使用您最喜欢的XHR库将GeoJSON文件加载到脚本中,使用检索到的数据定义一个L.GeoJSON
层并将其添加到layercontrol中。代码:
// Create the layercontrol and add it to the map
var controlLayers = L.control.layers().addTo(map);
// Loading a GeoJSON file (using jQuery's $.getJSON)
$.getJSON('/my-folder/my-file.json', function (data) {
// Use the data to create a GeoJSON layer and add it to the map
var geojsonLayer = L.geoJson(data).addTo(map);
// Add the geojson layer to the layercontrol
controlLayers.addOverlay(geojsonLayer, 'My GeoJSON layer title');
});
Plunker上的一个工作示例:检查源代码,了解它是如何完成的。