October 13, 2024

Plotting Google Maps Using Folium in Python

Folium is a powerful Python library used to visualize data that has been manipulated in Python on interactive Leaflet maps. It allows you to create and customize maps with various markers, popups, and other features. While Folium does not directly plot Google Maps, it provides similar functionality by using OpenStreetMap and other tile sources.

1. Installing Folium

To get started with Folium, you’ll need to install the library using pip:

pip install folium

2. Creating a Basic Map

Creating a basic map centered at a specific location is straightforward with Folium. You can specify the latitude and longitude coordinates to center the map.

2.1. Example: Creating a Basic Map

import folium

# Define the coordinates of the location (e.g., New York City)
latitude = 40.7128
longitude = -74.0060

# Create a map centered at the specified location
map = folium.Map(location=[latitude, longitude], zoom_start=12)

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

# Display the map inline (if using a Jupyter notebook)
map

This example creates a map centered on New York City with a zoom level of 12. The map is saved as an HTML file and can also be displayed directly if you are using a Jupyter notebook.

3. Adding Markers to the Map

You can enhance your map by adding markers at specific locations. Folium allows you to customize these markers with popups, icons, and more.

3.1. Example: Adding Markers

import folium

# Define the coordinates of the location
latitude = 40.7128
longitude = -74.0060

# Create a map centered at the specified location
map = folium.Map(location=[latitude, longitude], zoom_start=12)

# Add a marker to the map
folium.Marker([latitude, longitude], popup="New York City", icon=folium.Icon(color='blue')).add_to(map)

# Add another marker with a custom icon
folium.Marker([40.730610, -73.935242], popup="Marker with custom icon", icon=folium.Icon(icon='cloud')).add_to(map)

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

# Display the map inline (if using a Jupyter notebook)
map

In this example, two markers are added to the map: one at the coordinates for New York City and another at a nearby location. Each marker can have a popup and a customized icon.

4. Drawing Shapes on the Map

Folium also allows you to draw shapes such as circles, polygons, and rectangles on the map to highlight specific areas.

4.1. Example: Drawing Circles

import folium

# Define the coordinates of the location
latitude = 40.7128
longitude = -74.0060

# Create a map centered at the specified location
map = folium.Map(location=[latitude, longitude], zoom_start=12)

# Add a circle to the map
folium.Circle(
    radius=500,
    location=[latitude, longitude],
    popup="Center of NYC",
    color="crimson",
    fill=True,
).add_to(map)

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

# Display the map inline (if using a Jupyter notebook)
map

This example adds a circle to the map centered at the specified coordinates with a radius of 500 meters. The circle is filled with color and includes a popup.

5. Using Different Tile Layers

Folium allows you to change the tile layer of your map. This means you can switch from the default OpenStreetMap tiles to others like Stamen Terrain, Stamen Toner, or even custom tiles.

5.1. Example: Changing the Tile Layer

import folium

# Define the coordinates of the location
latitude = 40.7128
longitude = -74.0060

# Create a map centered at the specified location with a different tile layer
map = folium.Map(location=[latitude, longitude], zoom_start=12, tiles='Stamen Terrain')

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

# Display the map inline (if using a Jupyter notebook)
map

This example creates a map with the “Stamen Terrain” tile layer, which provides a different visual style compared to the default tiles.

6. Adding a Layer Control

You can add multiple tile layers to the map and allow the user to switch between them using the layer control feature.

6.1. Example: Adding Layer Control

import folium

# Define the coordinates of the location
latitude = 40.7128
longitude = -74.0060

# Create a map centered at the specified location
map = folium.Map(location=[latitude, longitude], zoom_start=12)

# Add multiple tile layers
folium.TileLayer('Stamen Terrain').add_to(map)
folium.TileLayer('Stamen Toner').add_to(map)
folium.TileLayer('Stamen Watercolor').add_to(map)

# Add layer control to switch between tile layers
folium.LayerControl().add_to(map)

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

# Display the map inline (if using a Jupyter notebook)
map

This example adds multiple tile layers to the map and a layer control that allows the user to switch between them.

7. Displaying the Map in a Jupyter Notebook

If you are using a Jupyter notebook, you can display the map inline directly without saving it as an HTML file.

7.1. Example: Displaying Map Inline in Jupyter Notebook

import folium

# Define the coordinates of the location
latitude = 40.7128
longitude = -74.0060

# Create a map centered at the specified location
map = folium.Map(location=[latitude, longitude], zoom_start=12)

# Display the map inline in a Jupyter notebook
map

Folium is a powerful library that allows you to create interactive maps in Python with minimal effort. While it does not directly use Google Maps, it provides similar functionality with various tile layers, markers, shapes, and other features. By using Folium, you can create rich, interactive visualizations of geographic data and display them in Jupyter notebooks or save them as HTML files for use on the web.