我有一个GeoJSON FeatureCollection对象,其中包含超过2000个功能。以GeoJSON对象,每个特征是一个种类的部分,像这样:Leaflet:如何在没有样式属性的GeoJSON对象中设计2000+个点?
{
"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
]
}
},
在我的实际数据中,有一个总的约38种类(每个特征仅被分配给一个类)。
在我的情况中使用JavaScript Switch语句是一种实用的解决方案,以便赋予每个点自己的样式?或者,还有更好的方法?
我做这样的事情在我的代码:
L.geoJson(mygeoJson, {
onEachFeature: function (feature, layer){
layer.bindPopup(L.Util.template(popupTemplate, feature.properties));
},
pointToLayer: function (feature, latlng){
return L.circleMarker(latlng, gjsonOptions);
},
// 3 of the 38 categories are listed here as an example
style: function(feature){
switch(feature.properties.category){
case 'Electrical': return { color: '#fb8072'};
case 'Military': return { color: '#b3de69'};
case 'Aviation': return { color: '#80b1d3'};
}
}
}).addTo(map);
有可能是一个几种不同的方式来做到这一点。有没有办法可以修改GeoJSON来添加一个新的属性(这将只是十六进制颜色)?如果没有,你可以将它们作为对象存储在一个数组中,并使用另一个库(比如undersore或lodash)为你做繁重的工作。 –
如果你知道什么形状文件,我实际上正在形状文件,然后将其转换为GeoJSON格式。我想我已经编辑了这些形状文件,所以他们有一个新的六角色字段。 – redshift
这将是我的建议。那么你不需要任何其他库等,如果你卡住了或者只是评论这个线程 –