Generating a Random Map Marker with Folium

  • Share this:

Code introduction


This function uses the Folium library to add a marker with a popup content at a specified location on a map, and saves the generated map as an HTML file.


Technology Stack : Folium

Code Type : Function

Code Difficulty : Intermediate


                
                    
def generate_random_marker(location, popup_content):
    """
    This function generates a random marker on a map using Folium.

    Parameters:
    location (list): The geographic coordinates (latitude, longitude) for the marker.
    popup_content (str): The content to be displayed in the popup when the marker is clicked.
    """
    import folium

    # Create a map object centered on the provided location
    m = folium.Map(location=location, zoom_start=12)

    # Add a marker to the map at the provided location with the given popup content
    folium.Marker(location=location, popup=popup_content).add_to(m)

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

# Example usage:
# generate_random_marker([34.052235, -118.243683], "This is a random marker!")                
              
Tags: