使用Mapbox默认设置Leaflet图层控制" Off"

时间:2015-02-26 21:23:57

标签: javascript leaflet mapbox

我们已经实现了一个图层控件,就像我们的应用程序下面的Mapbox示例一样。它工作得很好,但我们希望将某些图层默认为切换" off"。有什么帮助吗?

这是我们的代码:

    function displayLayerMaps(init_lat, init_lon, addr, parcel_data) {

    //$("#layer-map").html("");
    //$("#layer_map_ui").html("");
    var $layers = $('#layer_map_ui').empty();       

    if (layer_map) {
        layer_map.remove();
    }

    layer_map = L.mapbox.map('layer-map', "dirturban.k6hmjki6").setView([init_lat, init_lon], 22);
    layer_map.scrollWheelZoom.disable();

    //addLayer(L.mapbox.tileLayer('dirturban.7d1a3087'), 'Parcels', 7, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.84de85b4'), 'Satellite View', 1, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.460b53c7'), 'Detailed Zoning', 2, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.7adcb589'), 'Incentive Zoning', 3, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.c05e9c5a'), 'Urban Village', 4, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.b92c50a4'), 'Pavement Edge', 5, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.6fa753d7'), 'Park Boundaries', 6, $layers, layer_map);
    addLayer(L.mapbox.tileLayer('dirturban.e2744409'), 'City Defined Trees', 7, $layers, layer_map);
    // and parcels

    addMarker(layer_map,init_lat,init_lon, addr, "small", "#cc0000");

渲染,它看起来像这样:

http://i.imgur.com/PiEP88v.jpg

谢谢,

史蒂夫

1 个答案:

答案 0 :(得分:0)

只需创建L.mapbox.map的实例,并使用null作为mapid参数:

L.mapbox.accessToken = 'pk.eyJ1IjoicGF1bC12ZXJob2V2ZW4iLCJhIjoiZ1BtMWhPSSJ9.wQGnLyl1DiuIQuS0U_PtiQ';

var map = L.mapbox.map('mapbox', null, {
    'center': [0, 0],
    'zoom': 0
});

然后创建您需要的地图实例,并将其添加到您的控件中,而不要在其上调用.addTo方法:

var layers = {
    Streets: L.mapbox.tileLayer('examples.map-i87786ca'),
    Outdoors: L.mapbox.tileLayer('examples.ik7djhcc'),
    Satellite: L.mapbox.tileLayer('examples.map-igb471ik')
}

L.control.layers(layers).addTo(map);

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

代码主要来自:https://www.mapbox.com/mapbox.js/example/v1.0.0/toggle-baslayers/