传单和Mapbox:使用L.geoJson设置Mapbox标记的样式? [英] Leaflet and Mapbox: Styling mapbox markers using L.geoJson?
问题描述
我正在尝试在L.geoJson函数中使用Mapbox的简单样式语法设置标记颜色的样式,如下所示:
I am trying to style my marker color using Mapbox's simple-style syntax within a L.geoJson function like so:
L.geoJson(myData, {
pointToLayer: L.mapbox.marker.style,
style: function(feature){
return { 'marker-color': '#ffcc00' }
}
});
根据地图框文档,您可以在L.geoJson中使用L.mapbox.marker.style作为mapbox的默认标记,但是我似乎无法弄清楚如何用其他颜色设置样式.
According to mapbox docs, you can use L.mapbox.marker.style within L.geoJson for mapbox's default markers, but I can't seem to figure out how to style it with a different color.
这里有一个类似的问题,但我无法在我的客户端代码中运行它.
There was a similar question posted here but I could not get it work in my client-side code.
有人知道该怎么做吗?这是一个演示小提琴来开始.
Does anyone know how to do this? Here's a demo fiddle to get started.
注意:我知道可以将标记属性添加到正在使用的实际数据中,但是我需要能够在客户端代码中设置标记的样式,因为我无法访问geoJson featureCollection
推荐答案
由于您不能依赖已定义样式属性的GeoJSON数据,因此只需将自己的样式修补",然后再将每个功能传递给
Since you cannot rely on your GeoJSON data having styling properties already defined, you simply need to "patch" it with your own style before passing each feature to 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);
}
});
演示: http://jsfiddle.net/W763Z/6/
这篇关于传单和Mapbox:使用L.geoJson设置Mapbox标记的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!