如何在 python 中使用 folium 将标记添加到热图?

问题描述 投票:0回答:1

我正在尝试根据坐标和坐标权重列表制作热图。我现在想将权重值添加到热图集群中,以便我可以看到给定集群区域中有多少人。

我能够使用下面的代码创建热图,但是当我尝试添加plugin.MarkerCluster 部分时,我陷入了困境。如果我将列表 lats_longs 添加到标记集群,它只会给我错误:

    import folium
    from folium.plugins import HeatMap
    from folium import plugins
    map_obj = folium.Map(location=[39.057880, -77.459930], zoom_start=10)
    lats_longs = [
        [38.91072, -77.01666, 1],
        [38.93577, -77.05921, 1],
        [38.92039, -77.03856, 1],
        [38.93684, -77.09312, 1],
        [38.96083, -77.60435, 3],
        [38.74622, -77.48911, 1],
        [39.17396, -77.73253, 1],
        [39.09063, -77.88877, 1],
        [39.10795, -77.78462, 1],
        [38.92919, -77.93868, 1],
        [39.03957, -77.48161, 30],
        [39.00149, -77.51559, 3],
        [38.89222, -77.50983, 3],
        [38.81329, -77.63394, 1],
        [39.13953, -77.65486, 3],
        [39.01413, -77.39991, 1],
        [39.04736, -77.38648, 2],
        [38.96916, -77.46316, 2],
        [38.86936, -77.63979, 1],
        [38.98372, -77.38276, 8],
        [38.9266, -77.39119, 6],
        [39.05227, -77.60358, 6],
        [39.16004, -77.52185, 13],
        [38.96097, -77.34247, 5],
        [38.93112, -77.35139, 4],
        [39.0048, -77.10248, 1],
        [39.00335, -77.03544, 1],
        [39.2569, -76.79174, 1],
        [39.4959, -77.45401, 1],
        [39.36158, -77.66193, 2],
        [38.83726, -77.34201, 2],
        [38.87937, -77.37677, 1],
        [38.75493, -77.31016, 1],
        [38.86296, -77.19387, 1],
        [39.00445, -77.3081, 3],
        [38.89743, -77.25287, 1],
        [38.88659, -77.09473, 3],
        [38.87499, -77.12261, 1],
        [38.89291, -77.07257, 1],
        [38.79007, -77.0812, 1],
        [39.15107, -78.27722, 1],
        [39.17532, -77.97529, 1],
        [37.5844, 77.51805, 1],
        [39.31515, -78.04753, 1],
        [39.25786, -77.86259, 1],
        [39.26838, -77.78963, 1]
    ]
    # plugins.MarkerCluster(lats_longs).add_to(map_obj)
    HeatMap(lats_longs).add_to(map_obj)
    map_obj.save("heatmap.html")

heatmap folium markerclusterer folium-plugins
1个回答
0
投票

你的目的有点不明确。我知道您想要在自己的图层中表示热图和标记簇。我只需要标记簇的纬度和经度以及弹出信息的人数。请参阅此处了解基本标记簇描述。

# plugins.MarkerCluster(lats_longs).add_to(map_obj)
marker_cluster = plugins.MarkerCluster(
    name='heatmap',
    overlay=True,
    control=False,
    icon_create_function=None
)

for k in lats_longs:
    location = k[0], k[1]
    marker = folium.Marker(location=location)
    popup = '{} Persons'.format(k[2])
    folium.Popup(popup, parse_html=True, max_width=100).add_to(marker)
    marker_cluster.add_child(marker)

marker_cluster.add_to(map_obj)
HeatMap(lats_longs).add_to(map_obj)
folium.LayerControl().add_to(map_obj);

#map_obj.save("heatmap.html")
map_obj

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.