如何在leaflet.js中切换初始图层可见性

时间:2014-11-21 07:55:14

标签: leaflet

我在leaflet.js中创建了一个包含多个图层的地图,但无法解决如何将一个图层切换为最初出现在地图上的问题。

Here is the link

我确信它相当简单,但无法理解。

1 个答案:

答案 0 :(得分:6)

声明图层后,您需要在地图中添加一个图层,就像使用图块处理器一样。请参阅下面的代码。这应该工作。图层控件将确定图层可见/添加到地图并在列表中进行检查。

var map = L.map('map').setView([52.814172, -2.079479], 9);

L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.jpg', {
    maxZoom: 18,
    attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.'
}).addTo(map);

// Left out layer-declarations, they are HUGE

anti_soc.addTo(map); // here it is

var overlayMaps = {
    "Anti-social behaviour" :anti_soc,
    "Criminal damage and arson": crim_dam,
    "Burglary": burg, 
    "Violence and sexual offences": viol,
    "Other theft": other_theft,
    "Vehicle crime": veh_crime,
    "Shoplifting": shoplift,
    "Public order": pub_ord, 
    "Robbery": robb,
    "Bicycle theft": bikes,
    "Drugs": drugs,
    "Theft from the person": theft_person,
    "Other crime": other_crime,
    "Possession of weapons": weap
};

L.control.layers(null, overlayMaps).addTo(map);