2016-11-15 112 views
0

我正在查看使地圖縮放時丟失部分圖層的功能。單張縮放地圖圖層

map.on('zoomend', onZoomend) 
function onZoomend(feature){ 
if(map.getZoom()<11) 
{map.addLayer("road_2"==feature.properties.density)}; 

if(map.getZoom()>11) 
{map.removeLayer("road_2"==feature.properties.density)}; 
    } 

我GeoJSON的文件是在面板圖層:

var overLayers = [ 
{ 
    group: "Layer", 
    collapsed: true, 
    layers: [ 
     { 
      name: "Road", 
      icon: iconByName('fuel_road'),    
      layer: L.geoJson(road,{style: style_road}) 
     }, 

文件GeoJSON的bulid說:

 var road = { 
    "type": "FeatureCollection", 

    "features": [ 
    { "type": "Feature", "properties": { "density" : "road_1".... 

    { "type": "Feature", "properties": { "density" : "road_2".... 

    { "type": "Feature", "properties": .... 

回答

1

我不知道如果我理解你的權利,但你要刪除放大時添加圖層並在縮小時添加圖層?

You just can use: 
if(map.getZoom()<11){ 
    mylayer.removeFrom(map); 
}else{  
    mylayer.addTo(map); 
} 

我理解你的意思嗎?

UPDATE:

您可以通過以GeoJSON層迭代和fillOpacity設置爲0隱藏一些geoms:

var geoJsonLayer = L.geoJson(myGeoJson).addTo(map); 
if(map.getZoom()<11){ 
geoJsonLayer.eachLayer(function(layer) { 
    if(layer.feature.properties.density == "road_"){ 
     layer.setStyle({fillOpacity:0}); 
    } 
}); 
} 
+0

那麼你明白了,但是這個圖層被放置在文件GeoJSON中。我想關閉文件的一部分,例如。高變焦時無法看到的多個多邊形。 –

+0

然後創建'L.LayerGroup's與您想要添加/從地圖中刪除的單張圖層組,將單個圖層添加到這些組(可能通過'L.GeoJSON'的'eachLayer'),然後添加/根據需要刪除這些組。 – IvanSanchez

0

更新:

是這樣有前途嗎?有什麼要改進的?

function style_road(feature) { 
    if ("road_1" == feature.properties.density) { 
     return { 
      weight: 3, 
      opacity:function() 
        {if (zoom > 13) {opacity:0}, 
        else (zoom < 13) {opacity:1}, 
        }, 
      color: 'red', 
     }; 
    } 
    else if ("road_2" == feature.properties.density){ 
     return { 
      weight: 1.5, 
      opacity: 1, 
      color: 'red',    
     }; 
    } 
    else { 
     return { 
      weight: 0.5, 
      opacity: 1, 
      color: 'red',    
     }; 
    } 
}