加载 geojson 多边形后,我想实现一个切换菜单来按值进行过滤。我假设实现是 similar to this cartodb map which uses SQL statements.
我想象我可以使用图层选择器并从菜单(例如 District 1)传递变量。
我的代码只是引入 15 个多边形的图层并对其进行样式设置。我想保留这个,但添加一个过滤器。在传单中如何做到这一点?非常感谢示例。
L.mapbox.accessToken = 'pk.eyJ1Ijoic2tvcmFzYXVydXMiLCJhIjoiaEdGTUZWTSJ9.osOC8tWU3bMaNprVNoEu7g';
var lamap = L.mapbox.map('mapid', 'skorasaurus.5eb85050')
.setView([34.000880, -118.04036], 10);
var featLayer = L.mapbox.featureLayer().addTo(lamap);
featLayer.loadURL('citycouncils.geojson');
function getMyColor(myargument) {
if (myargument === '1') {
return '#996767'
};
if (myargument === '2') {
return '#679967'
};
if (myargument === '10') {
return '#672799'
};
if (myargument === '14') {
return '#670099'
};
if (myargument === '15') {
return '#699799'
};
}
// styles each polygon based on its properties in the geojson file, using leaflet's setStyle
featLayer.on('ready', function() {
featLayer.eachLayer(function(polygon) {
polygon.bindPopup('District ' + polygon.feature.properties.DISTRICT);
console.log(typeof setStyle);
polygon.setStyle({
opacity: 0.55,
weight: 2,
opacity: 1,
fillOpacity: 0.55,
fillColor: getMyColor(polygon.feature.properties.DISTRICT),
color: 'white'
});
});
});
最佳答案
如documented过滤L.mapbox.FeatureLayer
非常简单。只需使用图层的 setFilter
方法设置过滤器方法即可。
L.mapbox.FeatureLayer
的setFilter
方法引用:
https://www.mapbox.com/mapbox.js/api/v2.1.5/l-mapbox-featurelayer/#section-featurelayer-setfilter
完整的工作示例位于 Mapbox 网站上: https://www.mapbox.com/mapbox.js/example/v1.0.0/multiple-marker-filters/
关于javascript - 如何使用 Leaflet 中的切换按钮过滤 geojson?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28681652/