問題描述
我有兩個 geoJSON 功能集合需要添加到地圖中,我還希望通過圖層可見性控制器打開和關閉它們,如 http://leafletjs.com/examples/layers-control.html
I have two geoJSON feature collections that I need to add to the map, and I also want them to be toggled on and off via the layer visibility controllers as shown in http://leafletjs.com/examples/layers-control.html
我該怎么做?
推薦答案
Leaflet的GeoJSON層L.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
There is also a very good tutorial on the usage of L.GeoJSON
, Leaflet's GeoJSON layer, which can be found here: http://leafletjs.com/examples/geojson.html and here is the reference for L.GeoJSON
: http://leafletjs.com/reference.html#geojson You already found the tutorial on L.control.layers
, here is the reference for it: http://leafletjs.com/reference.html#control-layers
其實做起來很簡單,只需要創建一個layercontrol,使用你喜歡的XHR庫加載一個GeoJSON文件到你的腳本中,使用檢索到的數據來定義一個L.GeoJSON
圖層并將其添加到圖層控件.在代碼中:
It's actually quite simple to do, it's just a matter of creating a layercontrol, loading a GeoJSON file into your script by using your favorite XHR library, use the retrieved data to defined a L.GeoJSON
layer and add it to the layercontrol. In code:
// 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=預覽
A working example on Plunker: http://plnkr.co/edit/tFVrrq?p=preview
這篇關于如何將兩個 geoJSON 特征集合添加到兩個圖層組中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!