如何将两个geoJSON要素集合添加到两个图层组中

thu*_*hak 4 javascript openstreetmap leaflet

我有两个geoJSON要素集需要添加到地图中,我还希望通过图层可见性控制器打开和关闭它们,如http://leafletjs.com/examples/layers-control.html中所示.

我怎样才能做到这一点?

iH8*_*iH8 10

还有一个关于L.GeoJSONLeaflet的GeoJSON层使用的非常好的教程,可以在这里找到:http://leafletjs.com/examples/geojson.html,这里有以下参考L.GeoJSON:http://leafletjs.com/ reference.html#geojson您已经找到了该教程L.control.layers,以下是它的参考:http://leafletjs.com/reference.html#control-layers

这实际上非常简单,只需创建一个图层控件,使用您喜欢的XHR库将GeoJSON文件加载到脚本中,使用检索到的数据定义L.GeoJSON图层并将其添加到图层控件中.在代码中:

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

});
Run Code Online (Sandbox Code Playgroud)

关于Plunker的一个工作示例:http://plnkr.co/edit/tFVrrq?p = preview