【发布时间】:2018-08-12 16:07:13
【问题描述】:
我正在使用 Leaflet 和一些 GeoJSON 数据制作一张显示城市铁路运输历史的交互式地图。我的 GeoJSON 数据如下所示:
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": [...
]
}
....
我正在使用几个 FeatureCollections,其中包含一些功能。如您所见,每个要素的几何形状由 GeometryCollection(主要由 LineStrings 组成)定义。
我想使用 Leaflet 根据其成员设置每个几何体(GeometryCollections 中的 LineStrings)的样式。目前我只能通过 Feature 设置整个 GeometryCollection 的样式:
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 为每个几何图形创建了一条路径。
干杯
【问题讨论】:
标签: javascript leaflet geojson