Javascript 如何在叶状标记聚类上显示平均值而不是计数?
我正在使用python中的folium包为我的数据显示MarkerCluster 如果没有完全放大,簇看起来不错,但它们似乎显示了该簇内子标记数的计数。我理解为什么这是默认行为,但出于我的目的,我真的希望集群显示给定集群中每个标记的平均值,以达到缩放级别 以下是我目前的代码:Javascript 如何在叶状标记聚类上显示平均值而不是计数?,javascript,python,leaflet,folium,Javascript,Python,Leaflet,Folium,我正在使用python中的folium包为我的数据显示MarkerCluster 如果没有完全放大,簇看起来不错,但它们似乎显示了该簇内子标记数的计数。我理解为什么这是默认行为,但出于我的目的,我真的希望集群显示给定集群中每个标记的平均值,以达到缩放级别 以下是我目前的代码: folium_map = folium.Map(location=[33.97810188618428, -118.2155395906348]) mc = MarkerCluster() for p in points:
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会使用一些参数,让您发送“count”或“average”,但事实似乎并非如此。我谨慎地乐观地认为,有人能够提出一个相当简单的修复方案,不涉及分叉传单(js库的对开本是建立在)和编辑js源代码。我不是第一个想在MarkerClusters上显示与总和不同的度量的人,特别是集群中标记值的平均值 要自定义标记簇
图标创建功能
功能,以下示例演示如何覆盖标记标签以显示自定义值而不是默认值(簇中标记的计数):
现在在Folium中,一旦标记对象被实例化(其中population
是一个自定义属性)
可以通过JavaScript访问其自定义属性,如下所示:
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (var i = 0; i < markers.length; i++) {
sum += markers[i].options.props.population;
}
var markers=cluster.getAllChildMarkers();
var总和=0;
对于(var i=0;i
总而言之,下面是一个示例,演示了如何:
- 通过标记传递自定义属性
- 计算每个簇标记的自定义标记特性的平均值
- 显示簇标记的自定义标签
#%%
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
#%%
导入json
进口叶
从叶输入标记
从folium.plugins导入MarkerCluster
从jinja2导入模板
带道具的类标记(标记):
_模板=模板(u“”
{%macro脚本(this,kwargs)%}
var{{this.get_name()}}=L.marker(
[{this.location[0]},{{{this.location[1]}},
{
图标:新的L.icon.Default(),
{%-if this.draggable%}
真的,
自动扫描:是的,
{%-endif%}
{%-if this.props%}
道具:{{this.props}
{%-endif%}
}
)
.addTo({{this.\u parent.get\u name()}});
{%endmacro%}
""")
def u uu init u uuu(自我、位置、弹出窗口=None、工具提示=None、图标=None、,
draggable=False,props=None):
超级(MarkerWithProps,self)。\uuuu初始化(位置=位置,弹出窗口=弹出窗口,工具提示=工具提示,图标=图标,可拖动=可拖动)
self.props=json.loads(json.dumps(props))
map=folium.map(位置=[44,-73],缩放开始=4)
标记数据=(
{
“位置”:[40.67,-73.94],
"人口":200
},
{
“位置”:[44.67,-73.94],
"人口":300
}
)
图标\创建\函数=“”
功能(群集){
var markers=cluster.getAllChildMarkers();
var总和=0;
对于(var i=0;i
结果
marker = MarkerWithProps(
location=marker_item['location'],
props = { 'population': marker_item['population']}
)
marker.add_to(marker_cluster)
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