传单:如何从单个集合切换GeoJSON要素属性? [英] Leaflet: How to toggle GeoJSON feature properties from a single collection?

查看:82
本文介绍了传单:如何从单个集合切换GeoJSON要素属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a single GeoJSON object that contains over 2000+ features and each feature is part of one category (i.e. "Electrical", "Military", etc). There are a total of about 38 categories.

这是我的收藏的模式示例:

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]}}

这是我的L.geoJson函数,它遍历 集合:

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);

如何为我的L.control函数分配每个category属性,以便用户可以打开/关闭集合中的各个类别?如果我将每个类别都设为数据集和一个单独的geoJSOn图层,但是要完成所有38个类别的工作量太大.

How can I assign each category property to my L.control function so the user can toggle on/off the various categories from the collection? I could do this if I made each category a dataset and an individual geoJSOn layer, but that's too much work to do all 38 categories.

我的尝试:

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

是否有更好的方法可以做到这一点?谢谢!

Is there a better way to do this? Thanks!

推荐答案

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

You can simply assign your layers within the onEachFeature function. You could even automate the creation of Layer Groups for each category.

结果:

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替换为映射而不是数组.

replaced overlays to be a mapping instead of an array.

这篇关于传单:如何从单个集合切换GeoJSON要素属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆