heatmapfoliummarkerclustererfolium-plugins

How do I add markers to a heatmap using folium in python?


I am trying to make a heatmap based on a list of coordinates and weights to the coordinates. I now want to add the value of the weights to the heatmap clusters so I can see how many people are in the given cluster area.

I was able to create the heat map with the code below, however when I tried adding plugin.MarkerCluster section I became stuck. If I added my list lats_longs to the Marker Cluster it just gave me errors:

    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")


Solution

  • Your purpose is a bit unclear. I understood that you want to represent heatmaps and marker clusters in their own layers. I only need latitude and longitude for the marker cluster and the number of people as pop-up information. See here for a basic marker cluster description.

    # 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