是否可以更改叶标记集群地图中使用的默认颜色?

Is it possible to change the default colors used in a folium marker cluster map?

我正在使用 folium 生成一些地图,我包括的功能之一是 markercluster 覆盖 - 因为我经常在地图上绘制数千个点。聚类将不同数量的 GPS 点组合在一起,并在地图图标顶部覆盖一个数字,表示有多少点被组合到该聚类中。默认情况下,集群中组合在一起的点越少,地图图标就会呈现绿色,而组合在一起的点越多,则越接近红色光谱。理想情况下,我想反转这个,这样当一个位置有很多合并点时,图标就会变成绿色。而当只有几个合并点时,颜色将为红色。我认为这需要在某个地方的 branca 模块中进行编辑,但我不确定并且通常不熟悉 branca 的工作原理。非常感谢任何帮助。

以下是通常如何创建标记集群的示例:

import folium
from folium.plugins import MarkerCluster
#Create the map image and establish the center point
mapImage = folium.Map(location=[40.165505, -99.788130], 
zoom_start=12, 
tiles='OpenStreetMap')

#Create the marker cluster group, which organizes all the gps points put into it
marker_cluster_group = MarkerCluster(name='Cluster Icons')
#This is just a reference to a default google mapping icon, purely optional
pointIcon_url = "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
#Create the icon object    
icon = folium.features.CustomIcon(pointIcon_url, icon_size=(15, 15))
#Create the marker/gps point and add it to the cluster group
folium.Marker([40.058377, -99.939192], icon=icon).add_to(marker_cluster_group)
#Adding the cluster group to the map image
marker_cluster_group.add_to(mapImage)

您可以为 MarkerCluster class 提供一个参数 icon_create_function,这将为群集图标设置样式:

https://github.com/python-visualization/folium/blob/8595240517135d1637ca4cf7cc624045f1d911b3/folium/plugins/marker_cluster.py#L31

您可以在此处查看该函数的示例:

https://github.com/Leaflet/Leaflet.markercluster#customising-the-clustered-markers

所以这是一个 Javascript 函数,您将其作为字符串提供给 folium。

在@Conengmo 的回复的帮助下,我能够获得我需要的信息并根据需要修改它以创建以下内容。

import folium
from folium.plugins import MarkerCluster
#Create the map image and establish the center point
mapImage = folium.Map(location=[40.165505, -99.788130], 
zoom_start=12, 
tiles='OpenStreetMap')

#Create a variable to store your javascript function (written as a string), which adjusts the default css functionality
#The below are the attributes that I needed for my project, but they can be whatever is needed for you
icon_create_function = """
    function(cluster) {
    var childCount = cluster.getChildCount(); 
    var c = ' marker-cluster-';

    if (childCount < 50) {
        c += 'large';
    } else if (childCount < 300) {
        c += 'medium';
    } else {
        c += 'small';
    }

    return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
    }
    """
#Create the marker cluster group, which organizes all the gps points put into it
marker_cluster_group = MarkerCluster(name='Cluster Icons', icon_create_function=icon_create_function)
#This is just a reference to a default google mapping icon, purely optional
pointIcon_url = "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
#Create the icon object    
icon = folium.features.CustomIcon(pointIcon_url, icon_size=(15, 15))
#Create the marker/gps point and add it to the cluster group
folium.Marker([40.058377, -99.939192], icon=icon).add_to(marker_cluster_group)
#Adding the cluster group to the map image
marker_cluster_group.add_to(mapImage)