我有两个 geoJSON 要素集合需要添加到地图中,并且我还希望通过图层可见性控制器打开和关闭它们,如下所示http://leafletjs.com/examples/layers-control.html
我怎样才能做到这一点?
还有一个非常好的使用教程L.GeoJSON
,Leaflet 的 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');
});
Plunker 上的一个工作示例:http://plnkr.co/edit/tFVrrq?p=preview
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)