Leaflet:如何从单个集合中切换 GeoJSON 特征属性?

Leaflet: How to toggle GeoJSON feature properties from a single collection?

我有一个包含 2000 多个特征的 GeoJSON 对象,每个特征都属于一个类别(即 "Electrical"、"Military" 等)。总共有大约 38 个类别。

Here's the schema example of my collection:

{"type":"Feature","properties":{"category":"Electrical","Name":"Plant No 1"},"geometry":{"type":"Point","coordinates":[81.73828125,62.59334083012024]}},{"type":"Feature","properties":{"category":"Electrical","Name":"Plane No 2"},"geometry":{"type":"Point","coordinates":[94.5703125,58.722598828043374]}},{"type":"Feature","properties":{"category":"Military","Name":"Base 1"},"geometry":{"type":"Point","coordinates":[104.4140625,62.91523303947614]}}

Here's my L.geoJson function that iterates through the collection:

var allPoints = L.geoJson(myCollection, {
    onEachFeature: function(feature, layer){
        layer.bindPopup(L.Util.template(popTemplate, feature.properties));
    },
    "stylel": function(feature){
        return { "color": legend[feature.properties.category]
    }
}}).addTo(map);

如何将每个 category 属性 分配给我的 L.control 函数,以便用户可以切换 on/off 集合中的各个类别? 如果我将每个类别设为一个数据集和一个单独的 geoJSON 层,我就可以做到这一点,但是完成所有 38 个类别的工作量太大了。

我的尝试:

L.control.layers({
    'Street Map': L.mapbox.tileLayer('mapbox.streets').addTo(map)
},{
    'Electrical': myCollection[feature.properties.category["Electrical"]],
    'Military': myCollection[feature.properties.category["Military"]]
});

有更好的方法吗?谢谢!

您可以简单地在 onEachFeature 函数中分配图层。您甚至可以为每个类别自动创建图层组。

结果:

var categories = {},
    category;

function onEachFeature(feature, layer) {
    layer.bindPopup(L.Util.template(popTemplate, feature.properties));
    category = feature.properties.category;
    // Initialize the category array if not already set.
    if (typeof categories[category] === "undefined") {
        categories[category] = [];
    }
    categories[category].push(layer);
}

// Use function onEachFeature in your L.geoJson initialization.

var overlays = {},
    categoryName,
    categoryArray;

for (categoryName in categories) {
    categoryArray = categories[categoryName];
    overlays[categoryName] = L.layerGroup(categoryArray);
}

L.control.layers(basemaps, overlays).addTo(map);

编辑:将 overlays 替换为映射而不是数组。

迭代您的 GeoJSON 集合并创建多个 L.GeoJSON 层,每个类别一个,并将它们作为叠加层添加到您的 L.Control.Layers 实例。

var controlLayers = L.control.layers({
    'Street Map': L.mapbox.tileLayer('mapbox.streets').addTo(map)
}).addTo(map);

// Object to store category layers
var overlays = {};

// Iterate the collection
collection.features.forEach(function (feature) {

    var category = feature.properties.category;

    // Check if there's already an overlay for this category
    if (!overlays[category]) {

        // Create and store new layer in overlays object
        overlays[category] = new L.GeoJSON(null, {
            'onEachFeature': function () {},
            'style': function () {}
        });

        // Add layer/title to control
        controlLayers.addOverlay(overlays[category], category); 
    }

    // Add feature to corresponding layer
    overlays[category].addData(feature);
});

这里有一个关于 Plunker 的例子:http://plnkr.co/edit/Zv2xwv?p=preview