Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在两个图层组中添加两个geoJSON要素集合_Javascript_Leaflet_Openstreetmap - Fatal编程技术网

Javascript 如何在两个图层组中添加两个geoJSON要素集合

Javascript 如何在两个图层组中添加两个geoJSON要素集合,javascript,leaflet,openstreetmap,Javascript,Leaflet,Openstreetmap,我有两个geoJSON功能集合需要添加到地图中,我还希望通过图层可见性控制器打开和关闭它们,如中所示 如何执行此操作?由于在加载GeoJSON时创建了一个层,因此可以将其作为覆盖层添加到层控件中(只需修改该示例并替换cities层。关于传单的GeoJSON层L.GeoJSON的使用也有一个很好的教程,可以在这里找到:L.GeoJSON:您已经找到了关于L.control.layers的教程,这里是它的参考: 这实际上非常简单,只需创建一个layercontrol,使用您最喜欢的XHR库将GeoJ

我有两个geoJSON功能集合需要添加到地图中,我还希望通过图层可见性控制器打开和关闭它们,如中所示


如何执行此操作?

由于在加载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上的一个工作示例:

检查源代码,了解它是如何完成的。