Mapping Out Requirements
I'm currently fleshing out the scope. I've pinpointed the primary goal: create Python code for interactive maps using Folium. Now, I'm defining the user's needs to tailor the script's features and overall ease of use. I'm focusing on simplicity, anticipating a broad user base.
Detailing the Implementation Steps
I've sketched out the procedural steps for the script. First, I'll incorporate a base map with San Francisco's coordinates. Next, I'll demonstrate adding a marker, customizable with icons, and including interactive popups and tooltips. FeatureGroups will be crucial for organizing the data. The objective is to make this process intuitive, allowing the user to adapt the code effortlessly.
Formulating the Python Snippet
Now, I'm working through the code generation. I've broken down the script into stages. First is importing folium, then initiating a map centered on San Francisco. Next, I am thinking of adding a specific marker for the Golden Gate Bridge, and how to incorporate feature groups to enhance interactivity. Further, the script will show how to add various elements, like circles. I'm focusing on simplicity, so users can learn quickly.
Codifying the Structure
I'm now structuring the code. I've formulated the basic imports and the initial map setup centered on San Francisco. Further, I'm integrating FeatureGroups to enable layer toggling, which will greatly enhance the map's interactive nature. Next, I will be adding markers and circles and putting it all together in a cohesive example.
Developing the Code's Skeleton
I am now focusing on the Python script. I'm importing folium, defining a base map, and preparing to add specific markers and circles. FeatureGroups will be integrated for layer control and user interaction. The objective is to make the code readily adaptable. I'm also including brief notes on installation for those who don't know the library.
Generating the Script Outline
I am now focusing on the complete code structure. I'm building a Python script that starts with imports, a base map centered in San Francisco, and incorporates markers, circles, and FeatureGroups for an interactive experience. A key emphasis is ease of adaptation, along with clear comments. The goal is a simple, informative example that users can immediately use.
Here is a complete, step-by-step guide and a Python script to generate an interactive map using Folium.
Prerequisites
First, you need to install the library. Open your terminal or command prompt and run:
pip install folium
The Python Script
This script creates a map of San Francisco, organizes data into layers (Landmarks vs. Parks), adds custom markers, and enables a layer control switch.
import folium
def generate_map():
# 1. Create the base map
# We center it on San Francisco with a specific zoom level.
# 'tiles' determines the background style (OpenStreetMap is default).
map_obj = folium.Map(location=[37.7749, -122.4194], zoom_start=13, tiles="OpenStreetMap")
# 2. Create FeatureGroups
# FeatureGroups allow us to organize items into layers that can be toggled on/off.
fg_landmarks = folium.FeatureGroup(name="Famous Landmarks")
fg_parks = folium.FeatureGroup(name="Parks & Nature")
# 3. Add Markers to the 'Landmarks' group
# Marker 1: Golden Gate Bridge
# Note: Popups render HTML, so you can add bold text or links.
folium.Marker(
location=[37.8199, -122.4783],
popup="<b>Golden Gate Bridge</b><br>The iconic suspension bridge.",
tooltip="Click for info",
icon=folium.Icon(color="red", icon="info-sign")
).add_to(fg_landmarks)
# Marker 2: Alcatraz Island
folium.Marker(
location=[37.8270, -122.4230],
popup="<b>Alcatraz Island</b>",
tooltip="Alcatraz",
icon=folium.Icon(color="darkblue", icon="cloud")
).add_to(fg_landmarks)
# 4. Add Shapes to the 'Parks' group
# Circle: Golden Gate Park
folium.Circle(
location=[37.7694, -122.4862],
radius=1500, # Radius in meters
popup="Golden Gate Park",
color="green",
fill=True,
fill_color="lightgreen"
).add_to(fg_parks)
# CircleMarker: Mission Dolores Park (Fixed radius in pixels regardless of zoom)
folium.CircleMarker(
location=[37.7596, -122.4269],
radius=20, # Radius in pixels
popup="Mission Dolores Park",
color="#3186cc",
fill=True,
fill_color="#3186cc"
).add_to(fg_parks)
# 5. Add the FeatureGroups to the Map
fg_landmarks.add_to(map_obj)
fg_parks.add_to(map_obj)
# 6. Add Layer Control
# This creates the menu in the top-right to toggle layers.
folium.LayerControl().add_to(map_obj)
# 7. Add a click lat/lon capture (Optional utility)
# Displays latitude and longitude in a popup when you click anywhere on the map.
map_obj.add_child(folium.LatLngPopup())
# 8. Save the map
output_file = "interactive_map.html"
map_obj.save(output_file)
print(f"Map generated successfully! Open '{output_file}' in your web browser.")
if __name__ == "__main__":
generate_map()
Key Features Explained
folium.Map(): This initializes the map.
location: Latitude and Longitude [lat, lon].
zoom_start: How close the camera is (1 = World view, 18 = Street view).
folium.Marker(): Adds a standard pin.
popup: Text/HTML that appears when you click the marker.
tooltip: Text that appears when you hover over the marker.
icon: Customizes the pin color and symbol (uses Bootstrap glyphicons by default).
folium.Circle() vs folium.CircleMarker():
Circle: The radius is in meters. As you zoom out, the circle gets smaller visually (represents a real physical area).
CircleMarker: The radius is in pixels. The dot stays the same size on your screen regardless of zoom level.
folium.FeatureGroup() & LayerControl():
- We created two groups ("Landmarks" and "Parks").
- We added markers/shapes to those groups instead of directly to the map.
LayerControl adds the icon in the top-right corner allowing user to uncheck specific layers.
- HTML Output: Folium compiles Python code into HTML, CSS, and JavaScript (Leaflet.js). You do not need a web server to run it; just double-click the
.html file.
How to run it
- Save the code above into a file named
mymap.py.
- Run
python mymap.py.
- Look for a file named
interactive_map.html in the same folder.
- Open that file in Chrome, Firefox, or Edge.