You can download this code by clicking the button below.
This code is now available for download.
This function creates a map based on a given location using the Folium library and optionally adds markers. The map is saved as an HTML file.
Technology Stack : Folium, Python
Code Type : Function
Code Difficulty : Intermediate
import folium
import random
def create_random_map(location, zoom_start, markers=None):
"""
This function creates a random map with markers using the Folium library.
"""
# Create a map object centered on the specified location with the specified zoom level
map_obj = folium.Map(location=location, zoom_start=zoom_start)
# If markers are provided, add them to the map
if markers:
for marker in markers:
folium.Marker(marker['location'],
popup=marker['popup'],
icon=folium.Icon(color=marker['color'])).add_to(map_obj)
# Save the map to an HTML file
map_obj.save('random_map.html')
# Example usage
markers = [
{'location': [34.0522, -118.2437], 'popup': 'Los Angeles', 'color': 'red'},
{'location': [40.7128, -74.0060], 'popup': 'New York', 'color': 'blue'}
]
create_random_map([34.0522, -118.2437], 10, markers)