Marker Clusters on a python folium map

folium_marker_cluster

Skill - Marker Clusters on a python folium map

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


In this post we will learn how to create a marker cluster in folium maps using the MarkerCluster plugin in the folium library. See this post to learn about folium libary basics.

folium Marker Cluster plugin documentation can be found here

Code

import folium
from folium.plugins import MarkerCluster
import random

# create a map object
mapObj = folium.Map([24.21, 81.08], zoom_start=6)

# creating random marker locations for Marker Cluster.
# each list item should be in the format [lat, long]
markerLocs = [[random.uniform(18, 29), random.uniform(73, 85)]
              for x in range(100)]

mCluster = MarkerCluster(name="Markers Demo").add_to(mapObj)

for pnt in markerLocs:
    folium.Marker(location=[pnt[0], pnt[1]],
                  popup="pnt - {0}, {1}".format(pnt[0], pnt[1])).add_to(mCluster)

folium.LayerControl().add_to(mapObj)

# save the map object as html
mapObj.save("output.html")

Points to remember

  • By mentioning the name input to the MarkerCluster function, we can control the name of the Marker Cluster layer in Layer switcher

marker_cluster_demo

Video

The video for this post can be found here


References


Table of Contents

Comments