我正在使用python中的folium包来显示我的数据的MarkerClusters。
当您没有完全放大群集时,群集看起来不错,但是它们似乎显示了该群集中子标记数量的计数。我理解为什么这是默认行为,但是出于我的目的,我真的希望集群显示给定集群内每个标记的缩放级别的平均值。
这是我现在存在的代码:
folium_map = folium.Map(location=[33.97810188618428, -118.2155395906348])
mc = MarkerCluster()
for p in points:
marker = build_folium_marker(p['f_name'], p['value'], p['lat'], p['lng'])
mc.add_child(marker)
folium_map.add_children(mc)
folium_map.save('folium_marker_cluster_map.html')
在理想情况下,MarkerCluster会采用一些参数,使您可以发送“计数”或“平均值”,但事实并非如此。我谨慎地乐观,有人将能够提出一个相当简单的修复程序,该修复程序不涉及派生传单(构建js库文件)和编辑JS源代码。我不能是第一个想要在MarkerClusters上显示与总和不同的指标的人,尤其是集群中标记值的平均值。
答案 0 :(得分:0)
要自定义标记群集icon_create_function
功能,以下示例演示了如何覆盖标记标签以显示自定义值而不是默认值(群集中标记的数量):
icon_create_function = '''
function(cluster) {
return L.divIcon({
html: '<b>' + 123 + '</b>',
className: 'marker-cluster marker-cluster-small',
iconSize: new L.Point(20, 20)
});
}
'''
marker_cluster = MarkerCluster(icon_create_function=icon_create_function)
现在轮到通过标记传递自定义属性了,在Folium中默认标记不支持它,但是可以引入以下标记类来扩展Marker
类:
class MarkerWithProps(Marker):
_template = Template(u"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.marker(
[{{this.location[0]}}, {{this.location[1]}}],
{
icon: new L.Icon.Default(),
{%- if this.draggable %}
draggable: true,
autoPan: true,
{%- endif %}
{%- if this.props %}
props : {{ this.props }}
{%- endif %}
}
)
.addTo({{this._parent.get_name()}});
{% endmacro %}
""")
def __init__(self, location, popup=None, tooltip=None, icon=None,
draggable=False, props = None ):
super(MarkerWithProps, self).__init__(location=location,popup=popup,tooltip=tooltip,icon=icon,draggable=draggable)
self.props = json.loads(json.dumps(props))
实例化标记对象后,现在在Folium中(其中population
是自定义属性)
marker = MarkerWithProps(
location=marker_item['location'],
props = { 'population': marker_item['population']}
)
marker.add_to(marker_cluster)
可以通过JavaScript这样访问其自定义属性:
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (var i = 0; i < markers.length; i++) {
sum += markers[i].options.props.population;
}
总而言之,这是一个演示如何进行以下操作的示例:
示例
#%%
import json
import folium
from folium import Marker
from folium.plugins import MarkerCluster
from jinja2 import Template
class MarkerWithProps(Marker):
_template = Template(u"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.marker(
[{{this.location[0]}}, {{this.location[1]}}],
{
icon: new L.Icon.Default(),
{%- if this.draggable %}
draggable: true,
autoPan: true,
{%- endif %}
{%- if this.props %}
props : {{ this.props }}
{%- endif %}
}
)
.addTo({{this._parent.get_name()}});
{% endmacro %}
""")
def __init__(self, location, popup=None, tooltip=None, icon=None,
draggable=False, props = None ):
super(MarkerWithProps, self).__init__(location=location,popup=popup,tooltip=tooltip,icon=icon,draggable=draggable)
self.props = json.loads(json.dumps(props))
map = folium.Map(location=[44, -73], zoom_start=4)
marker_data =(
{
'location':[40.67, -73.94],
'population': 200
},
{
'location':[44.67, -73.94],
'population': 300
}
)
icon_create_function = '''
function(cluster) {
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (var i = 0; i < markers.length; i++) {
sum += markers[i].options.props.population;
}
var avg = sum/cluster.getChildCount();
return L.divIcon({
html: '<b>' + avg + '</b>',
className: 'marker-cluster marker-cluster-small',
iconSize: new L.Point(20, 20)
});
}
'''
marker_cluster = MarkerCluster(icon_create_function=icon_create_function)
for marker_item in marker_data:
marker = MarkerWithProps(
location=marker_item['location'],
props = { 'population': marker_item['population']}
)
marker.add_to(marker_cluster)
marker_cluster.add_to(map)
#m.save(os.path.join('results', '1000_MarkerCluster0.html'))
map
结果