【问题标题】:Displaying styles stored in a GeoJson File and making lines appear as lines instead of markers显示存储在 GeoJson 文件中的样式并使线条显示为线条而不是标记
【发布时间】:2020-10-28 15:23:30
【问题描述】:

我一直试图让 Leaflet 使用 geojson 文件中描述的样式显示 geojson 文件,但我无法让它工作。下面的geojson显示我在那里有样式-OGR样式笔等,但我尝试使用样式函数(样式){return {color:data.properties.pen}}提取它们,但它给了我一个错误在控制台上 - 但没有足够的错误来匹配层数 - 所以我可以理解某些层可能没有“笔”属性,但没有任何层出现任何差异。

“特征”:[ {“类型”:“功能”,“属性”:{“图层”:“建筑物”,“子类”:“AcDbEntity:AcDb2dPolyline”,“EntityHandle”:“2ABF”,“OGR_STYLE”:“PEN(c:# ff7f00,p:"1.2g 0.72g 0.12g 0.72g")" }, "geometry": { "type": "LineString", "coordinates": [ [ -1.386274792183286, 54.907452998026585, 0.0 ], [ -1.38620119340016 /p>

事实上,正如上面的 geojson 所示,它实际上是一个几何图形——但显示的只是一个标记,这是我的第二个问题。谁能指出一些示例代码或任何可能对我有帮助的东西?

                $.getJSON(address, function(data) {
                //add GeoJSON layer to the map once the file is loaded
                layer[i] = L.geoJson(data, {style: function(styles) {
                                              return {color: data.properties.pen,
                                                      weight: data.properites.weight
                                                    };
                                              onEachFeature: onEachFeature
                                                                      }
                                            }).addTo(map);

谢谢。

【问题讨论】:

    标签: leaflet styles geojson markers


    【解决方案1】:

    将您的代码更改为:

    function onEachFeature(feature, layer) {
        if (feature.properties && layer instanceof L.Path) {
            layer.setStyle({
                color: feature.properties.pen,
                weight: feature.properites.weight
            });
        }
    }
    
    
    $.getJSON(address, function(data) {
        //add GeoJSON layer to the map once the file is loaded
        layer[i] = L.geoJson(data, {
            onEachFeature: onEachFeature
        }).addTo(map);
    });
    
    

    Leaflet GeoJson Tutorial

    【讨论】:

    • 谢谢。我已将您的代码放入 - 但出现错误 - Uncaught TypeError: layer.setStyle is not a function。所以我猜想那层应该是 layer[i] - 层的数组元素,但后来我得到了意想不到的令牌 [.所以我想我不能将数组传递给函数?
    • 那么你的geojson中也有点,因为标记没有.setStyle()的功能。我添加到 if && layer instanceof L.Path 然后它应该可以工作
    • Brill,这似乎行得通。我现在看到了我的几何形状,而不是正确的风格 - 只是基本的布鲁斯,但你给了我一个很好的开始,让我开始玩。谢谢。
    • 这是因为你没有pen 属性,只有OGR_STYLE 的值为PEN(c:#ff7f00,p:"1.2g 0.72g 0.12g 0.72g")。如果您认为这已经解决了您的问题,请接受答案
    【解决方案2】:

    Lorem ipsum dolor,坐在 amet consectetur adipisicing elit。 Laboriosam cum sed illum possimus blanditiis temporibus obcaecati consectetur,laborum dolores,aspernatur facilis adipisci commodi! Nihil, consectetur ea corporis quis laboriosam natus

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2012-01-19
      • 2018-11-26
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多