How to Plot the Google Map using folium package in Python

Using the folium package of Python, you can simply create and display interactive maps, including plotting a view like Google Map by introducing tiles. Here is an in-depth step-by-step explanation:

1. Install folium

Before using folium, you need to install it. Run the following command in your terminal or Jupyter notebook:v

pip install folium

2. Import the folium Package

After installation, import folium into your Python script or notebook:

import folium

3. Create a Base Map

The core component of any folium map is a base map. You can set the initial location and zoom level of the map. Example:

# Create a basic map
map = folium.Map(location=[latitude, longitude], zoom_start=zoom_level)
  • location: List of latitude and longitude coordinates to center the map.
  • zoom_start: Initial zoom level (integer; higher values = closer view).
# Centered on New York City
map = folium.Map(location=[40.7128, -74.0060], zoom_start=12)

4. Add Tiles (Map Layers)

folium gives you the ability to make use of several tile sets, like a Google Maps view. You can set the tile style at map creation time, or add it as a layer.
Common tile options:

  • "OpenStreetMap": Default tiles.
  • "Stamen Terrain": Terrain view.
  • "Stamen Toner": High-contrast black-and-white.
  • "CartoDB positron": A light-themed map.
  • "CartoDB dark_matter": A dark-themed map.

Example:

map = folium.Map(location=[40.7128, -74.0060], zoom_start=12, tiles='Stamen Terrain')

The same applies to Google Maps tiles. Folium does not include Google Maps due to licensing restrictions, but a custom tile URL can use similar functionality to Google Maps.

Now, let’s use some custom tiles:

map = folium.Map(location=[40.7128, -74.0060], zoom_start=12, tiles=None)

# Add Google Maps-like tiles
folium.TileLayer(
    tiles='https://mt1.google.com/vt/lyrs=r&x={x}&y={y}&z={z}',
    attr='Google Maps',
    name='Google Maps',
    overlay=True,
    control=True
).add_to(map)

5. Add Markers and Features

To make your map interactive, you can add markers and other features such as circles, polygons, or popups.

Add a Marker:

folium.Marker(
    location=[40.7128, -74.0060],
    popup='New York City',
    tooltip='Click for info',
).add_to(map)

Add a Circle:

folium.Circle(
    location=[40.7128, -74.0060],
    radius=500,  # in meters
    color='blue',
    fill=True,
    fill_color='cyan'
).add_to(map)

Add a Popup:

popup = folium.Popup("This is NYC", max_width=300)
folium.Marker([40.7128, -74.0060], popup=popup).add_to(map)

6. Save or Display the Map

  • Display in Jupyter Notebook: Simply use the map object in a Jupyter cell:
map
  • Save to an HTML File: You can save the map to an interactive HTML file:
map.save('map.html')

Complete Example

Here’s a full script to plot an interactive map centered on New York City with custom Google Maps-like tiles and markers:

import folium

# Create a base map centered on New York City
map = folium.Map(location=[40.7128, -74.0060], zoom_start=12, tiles=None)

# Add Google Maps-like tiles
folium.TileLayer(
    tiles='https://mt1.google.com/vt/lyrs=r&x={x}&y={y}&z={z}',
    attr='Google Maps',
    name='Google Maps',
    overlay=True,
    control=True
).add_to(map)

# Add a marker
folium.Marker(
    location=[40.7128, -74.0060],
    popup='New York City',
    tooltip='Click for info',
).add_to(map)

# Add a circle
folium.Circle(
    location=[40.7128, -74.0060],
    radius=1000,  # in meters
    color='blue',
    fill=True,
    fill_color='cyan'
).add_to(map)

# Save the map as an HTML file
map.save('nyc_map.html')

# Display in Jupyter Notebook
map

Output

  1. If using Jupyter Notebook, the interactive map will directly render in the output cell.
  2. If saved as nyc_map.html, it can be opened in a web browser to view the map.