2015-11-26 21 views
1

后,我用下面的代码添加一些指向一个地图,他们看起来棒极了。我还添加了一些没有问题的json多边形。删除自定义GeoJSON的标记(装有传单)变焦

当达到一定的缩放级别,我想点和多边形关闭。使用map.removeLayer(名称的多边形)关闭多边形完美,然后缩小我使用map.addLayer(多边形的名称),他们回来(使用'zoomend'和if语句)。

点状多边形做功能还没有反应过来的removeLayer功能。我也试过harvestPoints.setOpacity(0),它也不起作用。我应该使用什么代码将这些GeoJSON的标记“开”和“关”之类的多边形功能?

function onEachPoint(feature, layer) {    
    layer.bindPopup(feature.properties.MGNT_AREA.toString()); 
    layer.on('click', function (e) { layer.openPopup(); }); 
    layer.bindLabel(feature.properties.MGNT_AREA.toString(), { 
     noHide: true, 
     className: "my-label", 
     offset: [-2, -25] 
    }).addTo(map); 
}; 

var areaIcon = { 
    icon: L.icon({ 
     iconUrl: 'labels/MonitoringIcon.png', 
     iconAnchor: [20, 24] 
    }) 
}; 

var harvestPoints = new L.GeoJSON.AJAX('labels/dfo_areas_point.json', { 
    onEachFeature: onEachPoint, 
    pointToLayer: function (feature, latlng) { 
     return L.marker(latlng, areaIcon); 
    } 
}); 

回答

1

不知道到底是什么的根本原因您的问题,因为我们都在思念你引用你究竟怎么了点(标记)当您尝试从地图上删除。

通常情况下,应该有面和点(标记)来实现你所描述的(除去地图在某些缩放级别的图层,并将它们添加回在其他变焦)没有什么区别。

请注意setOpacity is a method for L.Markers,而您将它应用于您的geoJson图层组harvestPoints

什么可能发生是你(在你的onEachPoint函数最后一条指令)个别点(标记)添加到您的地图,而是试图从地图中删除图层组harvestPoints。因为它似乎永远不会添加到地图中,所以没有任何反应。

如果你想在同一时间你harvestPoints层组中打开/关闭所有的点,那么只需在添加/从地图上删除该层组/,而不是添加单个标记:

var harvestPoints = L.geoJson.ajax('labels/dfo_areas_point.json', { 
    onEachFeature: onEachPoint, 
    pointToLayer: function (feature, latlng) { 
           // make sure `areaIcon` is an OPTIONS objects 
     return L.marker(latlng, areaIcon); 
    } 
}).addTo(map); // Adding the entire geoJson layer to the map. 

map.on("zoomend", function() { 
    var newMapZoom = map.getZoom(); 

    if (newMapZoom >= 13) { 
     map.addLayer(harvestPoints); 
    } else { 
     // Removing entire geoJson layer that contains the points. 
     map.removeLayer(harvestPoints); 
    } 
}); 

边注:弹出窗口上点击默认打开的,你不应该需要在点击监听器添加的是什么?

演示:http://jsfiddle.net/ve2huzxw/62/

+0

这是因为添加.addTo(地图)到以GeoJSON层的端部一样简单。非常感谢。我希望这可以帮助别人。 – user5543624