【问题标题】:Is it possible to change the default colors used in a folium marker cluster map?是否可以更改叶标记集群地图中使用的默认颜色?
【发布时间】:2019-09-03 14:04:44
【问题描述】:

我正在使用 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)

【问题讨论】:

  • 是否可以更改叶标记集群地图中使用的默认颜色?是的。如何??请向我们展示一些代码,以便我们向您展示如何操作?

标签: python colors folium leaflet.markercluster


【解决方案1】:

您可以为MarkerCluster 类提供参数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。

【讨论】:

    【解决方案2】:

    在@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)
    

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 2013-08-29
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      相关资源
      最近更新 更多