Random Map Creation with Folium and Python

  • Share this:

Code introduction


This function uses the Folium library to create a map and adds a random marker to it. The color of the marker is randomly generated, and the center location and initial zoom level of the map are specified by the function parameters.


Technology Stack : Folium, Python

Code Type : Map Generation and Random Marking

Code Difficulty : Intermediate


                
                    
def create_random_map(location, zoom_start):
    import folium
    import random

    # Generate a random color
    random_color = '#' + ''.join([random.choice('0123456789ABCDEF') for _ in range(6)])

    # Create a map centered at the given location with a starting zoom level
    m = folium.Map(location=location, zoom_start=zoom_start)

    # Add a random marker to the map
    folium.Marker(location, popup='Random Location', icon=folium.Icon(color=random_color)).add_to(m)

    # Save the map to an HTML file
    m.save('random_map.html')

    return m                
              
Tags: