Leaflet 传单:如何从单个集合切换GeoJSON功能属性?

Leaflet 传单:如何从单个集合切换GeoJSON功能属性?,leaflet,mapbox,Leaflet,Mapbox,我有一个GeoJSON对象,包含超过2000个功能,每个功能都属于一个类别(即“电气”、“军事”等)。总共约有38类 以下是我的收藏的模式示例: 下面是我的L.geoJson函数,它遍历 收藏: 我如何将每个类别属性分配给我的L.control函数,以便用户可以打开/关闭集合中的各种类别?如果我将每个类别设置为数据集和单独的geoJSOn层,我可以这样做,但这太多的工作无法完成所有38个类别 我的尝试: L.control.layers({ 'Street Map': L.mapbox.

我有一个GeoJSON对象,包含超过2000个功能,每个功能都属于一个类别(即“电气”、“军事”等)。总共约有38类

以下是我的收藏的模式示例:

下面是我的L.geoJson函数,它遍历 收藏:

我如何将每个
类别
属性分配给我的L.control函数,以便用户可以打开/关闭集合中的各种类别?
如果我将每个类别设置为数据集和单独的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={},
类别
功能onEachFeature(功能,图层){
bindPopup(L.Util.template(popTemplate,feature.properties));
类别=feature.properties.category;
//初始化类别数组(如果尚未设置)。
if(类别类型[类别]=“未定义”){
类别[类别]=[];
}
类别[类别]。推送(图层);
}
//在L.geoJson初始化中使用函数onEachFeature。
变量覆盖={},
类别名称,
分类射线;
for(类别中的categoryName){
categoryArray=类别[categoryName];
覆盖层[categoryName]=L.layerGroup(categoryArray);
}
L.控制。图层(底图、覆盖图)。添加到(地图);

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

迭代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的一个示例:

您是否也可以提供一个在onEachFeature函数中分配层而不是自动分配层的示例?这可能是我目前比较容易掌握的一种方法。好的,我已经使用了这个方法(自动),但是需要一些帮助来打印控件中复选框旁边的类别名称。现在,只有数字。但这种切换确实有效。谢谢我的坏,
覆盖
应该是一个映射,而不是一个数组。我将更正上述代码以向您展示。谢谢,由于某些原因,我无法使其与mapbox地图一起使用。不管怎样,谢谢你的帮助。
L.control.layers({
    'Street Map': L.mapbox.tileLayer('mapbox.streets').addTo(map)
},{
    'Electrical': myCollection[feature.properties.category["Electrical"]],
    'Military': myCollection[feature.properties.category["Military"]]
});
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);
});