Leaflet 和 Mapbox:使用 L.geoJson 样式化 mapbox 标记?
Leaflet and Mapbox: Styling mapbox markers using L.geoJson?
我正在尝试在 L.geoJson 函数中使用 Mapbox 的简单样式语法来设置我的标记颜色样式,如下所示:
L.geoJson(myData, {
pointToLayer: L.mapbox.marker.style,
style: function(feature){
return { 'marker-color': '#ffcc00' }
}
});
根据 mapbox docs,您可以在 L.geoJson 中使用 L.mapbox.marker.style 作为 mapbox 的默认标记,但我似乎无法弄清楚如何使用不同的颜色设置样式。
有一个 similar question posted here 但我无法在我的客户端代码中使用它。
有人知道怎么做吗?这是开始的 demo fiddle。
注意:我知道可以将标记属性添加到正在使用的实际数据中,但我需要能够在客户端代码中设置标记的样式,因为我不会访问 geoJson featureCollection
由于您不能依赖已定义样式属性的 GeoJSON 数据,因此您只需 "patch" 在将每个特征传递给 L.mapbox.marker.style
之前使用您自己的样式
L.geoJson(myData, {
// Instead of passing directly L.mapbox.marker.style function,
// implement your own that will first "patch" the current feature
// with your desired styling properties.
pointToLayer: function (feature, latlng) {
feature.properties = {
"marker-size": "large",
"marker-color": "#cc0000"
};
// Finally call L.mapbox.marker.style with the "patched" feature.
return L.mapbox.marker.style(feature, latlng);
}
});
我正在尝试在 L.geoJson 函数中使用 Mapbox 的简单样式语法来设置我的标记颜色样式,如下所示:
L.geoJson(myData, {
pointToLayer: L.mapbox.marker.style,
style: function(feature){
return { 'marker-color': '#ffcc00' }
}
});
根据 mapbox docs,您可以在 L.geoJson 中使用 L.mapbox.marker.style 作为 mapbox 的默认标记,但我似乎无法弄清楚如何使用不同的颜色设置样式。
有一个 similar question posted here 但我无法在我的客户端代码中使用它。
有人知道怎么做吗?这是开始的 demo fiddle。
注意:我知道可以将标记属性添加到正在使用的实际数据中,但我需要能够在客户端代码中设置标记的样式,因为我不会访问 geoJson featureCollection
由于您不能依赖已定义样式属性的 GeoJSON 数据,因此您只需 "patch" 在将每个特征传递给 L.mapbox.marker.style
之前使用您自己的样式
L.geoJson(myData, {
// Instead of passing directly L.mapbox.marker.style function,
// implement your own that will first "patch" the current feature
// with your desired styling properties.
pointToLayer: function (feature, latlng) {
feature.properties = {
"marker-size": "large",
"marker-color": "#cc0000"
};
// Finally call L.mapbox.marker.style with the "patched" feature.
return L.mapbox.marker.style(feature, latlng);
}
});