我已经使用带有LayerGroup的切换按钮实现了geojson过滤器,但是我想知道是否有人使用地图上的鼠标单击成功完成了相同的行为。
示例:世界国家地图。单击“意大利”多边形将导致仅意大利可见。在意大利境外单击以再次显示所有国家。希望我的问题清楚。
只需挂接到图层的click事件,清除组并添加该单个图层即可。还要钩住地图,单击,删除单个图层,然后恢复其余的图层。这是一个快速n肮脏的示例:
// vars to store stuff
var geojson, source, selected;
// Load the collection
$.getJSON(url, function (collection) {
// Store collection for later use
source = collection;
// Create layer and add collection
geojson = L.geoJson(collection, {
// On each feature in collection
'onEachFeature': function (feature, layer) {
// Attach click handler
layer.on('click', function () {
// Set selected flag
selected = true;
// Clear the entire layer
geojson.clearLayers();
// Add the feature
geojson.addData(feature);
// Fit layer to map
map.fitBounds(layer.getBounds());
});
}
}).addTo(map);
});
// Attach to map click
map.on('click', function () {
// Check if something's selected
if (selected) {
// Clear the entire layer
geojson.clearLayers();
// Restore the collection
geojson.addData(source);
// Fit map to collection
map.fitBounds(geojson.getBounds());
}
});
这是有关Plunker的工作示例:http ://plnkr.co/edit/o5Q0p3?p=preview
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句