如何在Leaflet 1.0.3中更改LineString的geoJSON图层的样式 [英] How do I change the style of a geoJSON layer of LineStrings in Leaflet 1.0.3
问题描述
我正在尝试更改传单1.0.3中一层线的样式.我的图层是使用ajax调用和leaflet-ajax库生成的geoJSON图层.我可以在地图上看到我的图层,但是它继承了默认样式,而不是我尝试添加的样式.
I am trying to change the style of a layer of lines strings in leaflet 1.0.3. My layer is a geoJSON layer generated using an ajax call and the leaflet-ajax lib. I can see my layer on the map, but it inherits the default styling, not the styling I am trying to add.
var lines = new L.GeoJSON.AJAX('/rest/lines');
lines.setStyle({color:"#00000",weight:10}).addTo(map);
var overlays = {"Lines": lines};
L.control.layers(overlays).addTo(map);
推荐答案
您应该尝试定义一个函数,该函数将在每行加载到Leaflet中时对每行进行样式设置.
You should try to define a function which will style each line as it is loaded up into Leaflet.
通过此链接: https://github.com/Dominique92/Leaflet.GeoJSON.Ajax
...
new L.GeoJSON.Ajax(
<URL>, // GeoJson server URL.
{
argsGeoJSON: {
name: value, // GeoJson args pairs that will be added to the url with the syntax: ?name=value&...
...
}
bbox: <boolean>, // Optional: whether or not add bbox arg to the geoJson server URL
style: function(feature) { // Optional
return {
"<NAME>": <VALUE>, // Properties pairs that will overwrite the geoJson flow features properties
"<NAME>": feature.properties.<NAME>, // The value can be calculated from any geoJson property for each features.
...
};
}
}
).addTo(map);
...
这是我的代码,用于处理形状而不是线条,但是它应该以类似的方式工作:
This is my code, which is for shapes not lines, but it should work in a similar way:
geojson = L.geoJson(myGeoJson.features, {
onEachFeature: onEachFeature,
style: styleFeature,
}).addTo(myLeafletMap);
然后我有以下功能:
function onEachFeature(feature, layer) {
...
}
和
function styleFeature(feature){
return {
weight: 2.5,
opacity: 1,
color: getColour('blue'),
};
}
这篇关于如何在Leaflet 1.0.3中更改LineString的geoJSON图层的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!