我正在制作一张交互式地图,使用传单和一些GeoJSON数据显示城市铁路运输的历史。我的地理 JSON 数据如下所示:
var lines = [
{
"type": "FeatureCollection",
"name": "stadtmitte_gerresheim",
"startYear": "1902",
"endYear": "2019",
"lineColor": "#DD0000",
"trainID": "001",
"features": [
{
"type": "Feature",
"properties": {
"lineName": "U73",
"station1": "Universität Ost/Botanischer Garten",
"station2": "Gerresheim S",
"startYear": "2016",
"endYear": "2019",
"lineColor": "#DD0000",
},
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"lineID": "001",
"lineDescription": "Schleife am S-Bahnhof Gerresheim",
"coordinates": [...
]
},
{
"type": "LineString",
"lineID": "002",
"lineDescription": "Gerresheim S bis Ecke Schönaustraße/Heyestraße",
"coordinates": [...
]
}
....
我正在使用几个功能集合,其中包括一些功能。如您所见,每个要素的几何都是由 GeometryCollection(主要由 LineStrings(定义的。
我想使用Leaflet根据其成员设置每个几何图形(来自GeometryCollections的LineStrings(的样式。目前,我只能通过功能设置整个几何集合的样式:
L.geoJSON(route, {
filter: function(feature) {
if (checkYear(feature)) {
return true;
} else {
return false;
}
},
style: function(feature) {
switch (feature.properties.tunnel) {
case 'yes': return {color: "#ff0000", dashArray: "1,6"};
default: return {color: "#ff0000"}
}
},
pointToLayer: function (feature, latlng) {
getIconSize();
switch (feature.properties.underground) {
case 'yes': return L.marker(latlng, {icon: stadtbahnIcon});
default: return L.marker(latlng, {icon: strassenbahnIcon});
}
}
}).addTo(mymap);
}
我的问题:是否可以将单个样式函数传递给同一特征的不同几何图形,而不是将相同的样式函数传递给此功能中的所有几何图形?如果是这样,如何?我想这应该是可能的,因为传单为每个几何体创建了一个路径。
干杯
是否可以将单个样式函数交给同一特征的不同几何体?
不。
Leaflet 在 GeoJSON 中为每个Feature
创建一个L.Layer
实例,就在这里;style
回调函数的应用只是循环遍历与每个特征对应的L.Layer
,它不会向下钻取嵌套的L.LayerGroup
(此外,由于GeometryCollection
创建的L.Polyline
和L.Polygon
没有对原始 GeoJSON 功能的引用或其属性(。
我想你会想要使用TurfJS的geomEach
(或类似的东西(来预处理你的特征和几何。