【发布时间】:2015-11-02 13:00:56
【问题描述】:
我有一个包含 2000 多个特征的 GeoJSON 对象,每个特征都属于一个类别(即“电气”、“军事”等)。共有大约 38 个类别。
这是我的集合的架构示例:
{"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 函数,它遍历 收藏:
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 函数,以便用户可以打开/关闭集合中的各种类别?如果我将每个类别设为数据集和一个单独的 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"]]
});
有没有更好的方法来做到这一点?谢谢!
【问题讨论】: