如何使用 Leaflet 中的切换按钮过滤 geojson?

How to filter geojson using toggle buttons in Leaflet?

加载 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'
            });
        });
    });

As documented 过滤 L.mapbox.FeatureLayer 非常简单。这只是使用层的 setFilter 方法设置过滤方法的问题。

参考L.mapbox.FeatureLayersetFilter方法: 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/