Skill - Marker Clusters on a python folium map
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- Introduction to Folium for interactive maps in python
- Draw markers in python folium maps
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 theMarkerCluster
function, we can control the name of the Marker Cluster layer in Layer switcher
Video
The video for this post can be found here
References
- MarkerCluster plugin documentation - https://python-visualization.github.io/folium/plugins.html#folium.plugins.MarkerCluster
Comments
Post a Comment