Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Write a Python script to generate interactive maps using Folium.

  1. User

    Write a Python script to generate interactive maps using Folium.

  2. Assistant
    """
    Interactive Map Generator using Folium
    ======================================
    
    This script demonstrates how to create an interactive map using the Folium library.
    It plots markers for several major cities with popups containing information.
    
    Requirements:
    - Install Folium: pip install folium
    
    Usage:
    - Run the script to generate 'interactive_map.html'
    - Open the HTML file in a web browser to view the interactive map.
    
    Features:
    - Custom map center and zoom
    - Markers with popups and tooltips
    - Circle markers for visualization
    - Layer control for toggling elements
    - Full-screen plugin for better interaction
    """
    
    import folium
    from folium.plugins import Fullscreen, MarkerCluster
    
    # Define a list of locations (name, latitude, longitude, population)
    locations = [
        ("New York, USA", 40.7128, -74.0060, 8419000),
        ("London, UK", 51.5074, -0.1278, 8936270),
        ("Tokyo, Japan", 35.6895, 139.6917, 13929286),
        ("Sydney, Australia", -33.8688, 151.2093, 5312165),
        ("Paris, France", 48.8566, 2.3522, 2148271),
    ]
    
    # Create a base map centered on the world (average of locations or custom)
    map_center = [20, 0]  # Approximate world center
    m = folium.Map(
        location=map_center,
        zoom_start=2,
        tiles="OpenStreetMap",  # Default tileset
        attr="Interactive Map by Sherlock"
    )
    
    # Add fullscreen plugin
    Fullscreen().add_to(m)
    
    # Create a marker cluster for better performance with many markers
    marker_cluster = MarkerCluster().add_to(m)
    
    # Add markers for each location
    for name, lat, lon, pop in locations:
        # Standard marker with popup and tooltip
        folium.Marker(
            location=[lat, lon],
            popup=f"<b>{name}</b><br>Population: {pop:,}",
            tooltip=f"Click for info on {name}",
            icon=folium.Icon(color="blue", icon="info-sign")
        ).add_to(marker_cluster)
        
        # Add a circle to visualize population density (scaled)
        radius = min(pop / 100000, 50)  # Cap radius at 50px
        folium.Circle(
            location=[lat, lon],
            radius=radius * 100,  # Scale for map units (meters)
            popup=f"Population radius for {name}: {pop:,}",
            color="red",
            fill=True,
            fillOpacity=0.3
        ).add_to(m)
    
    # Add layer control to toggle markers and circles
    folium.LayerControl().add_to(m)
    
    # Save the map to an HTML file
    output_file = "interactive_map.html"
    m.save(output_file)
    print(f"Interactive map saved to '{output_file}'")
    print("Open it in your web browser to explore!")
    

    How to Run:

    1. Ensure Folium is installed: pip install folium
    2. Save the script as generate_map.py and run: python generate_map.py
    3. Open interactive_map.html in a browser.

    Customization Ideas:

    • Add your own data: Replace locations with your dataset (e.g., from CSV/Pandas).
    • Geocode addresses: Use folium.Geocoder() for dynamic locations.
    • Heatmap: Add folium.plugins.HeatMap() for density visualization.
    • Custom tiles: Change tiles to "Stamen Terrain" or others.

    This script produces a zoomable, pannable map with clusters for scalability. Enjoy mapping! 🗺️

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.