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

问题描述

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

功能”:[ {“ type”:“功能”,“ properties”:{“ Layer”:“建筑物”,“ 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

,

Lorem ipsum dolor,坐拥礼节。 Laboriosam暨sed illum possimus blanditiis temporibus obcaecati consectetur,laborum dolores,aspernatur facilis adipisci commodi!尼尔,身体的自然保护权