我用LayerGroup的切换按钮实现了geojson过滤器,但是我想知道是否有人使用地图上的鼠标来成功地使用同样的行为。
例子:世界各国地图。单击意大利多边形只会使意大利可见。单击“意大利境外”再次显示所有国家。希望我的问题很清楚。
发布于 2015-03-14 19:47:44
这只是一个连接到该层的单击事件,清除组并添加该单层的问题。也钩到地图点击,删除单层和恢复其余的。下面是一个快速-不干净的例子:
// 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());
}
});下面是柱塞的一个工作示例:http://plnkr.co/edit/o5Q0p3?p=preview
https://stackoverflow.com/questions/29053159
复制相似问题