【问题标题】:How to style each Geometry of a GeoJSON GeometryCollection individually using Leaflet如何使用 Leaflet 单独设置 GeoJSON GeometryCollection 的每个几何图形
【发布时间】: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


    【解决方案1】:

    是否可以将单独的样式函数传递给同一特征的不同几何?

    没有。

    Leaflet 在 GeoJSON 中为 Feature 创建一个 L.Layer 实例,right over here;以及style 回调函数just loops over the L.Layers corresponding to each feature 的应用,它不会深入嵌套L.LayerGroups(同样,L.Polylines 和L.Polygons 作为GeometryCollection don' 的结果创建没有对原始 GeoJSON 功能或其属性的引用)。

    我猜你会想使用TurfJS' geomEach(或类似的东西)来预处理你的特征和几何图形。

    【讨论】:

    • 谢谢,正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多