显示存储在 GeoJson 文件中的样式并使线条显示为线条而不是标记

Displaying styles stored in a GeoJson File and making lines appear as lines instead of markers

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

"features": [ { "type": "Feature", "properties": { "Layer": "Buildings", "SubClasses": "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.386201193400163,

事实上,如上面的 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);

谢谢。

将您的代码更改为:

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

痛苦本身,让肥胖的精英跟着它走。可当我们被谄媚的时代蒙蔽了双眼时,他追求的是艰苦的工作,痛苦的痛苦,他对轻而易举的优势感到沮丧!没什么,天生体力不支的他会追求那些