Getting started with Google Maps in Python
By: Elliott Saslow
Tags: Programming, Data Science, Python, Mapping, Web Development

Lets get started with google maps in python! We are going to cover making a basic map, adding different layers to the maps, and then creating driving directions!

Before this article, I did a quick tutorial with basemap in python. In this tutorial, I will cover using the google maps api in jupyter notebook. The first step here is to get an api key from google at this link. Once this is done, you may need to run the following command in terminal and restart jupyter:

jupyter nbextension enable --py gmaps

Once this is complete, you are ready to jump right into making beautiful maps!

Initially, we are going to want to read in our api key. To do this I completed the following steps:

Save the API key in a text file Write code to read in the key and save it as a variable

with open('apikey.txt') as f:

api_key = f.readline()

f.close

Once this is complete you are ready to use the google maps api. For reference, all the material in this tutorial comes from the documentation which can be found here.

You may also need to do a pip install gmaps prior to working in jupyter.

Once this is done, you can initialize the session with the command:

import gmaps

gmaps.configure(api_key='Your api key here')

After this, you can create a generic map by just creating a center location and zoom level:

new_york_coordinates = (40.75, -74.00)

gmaps.figure(center=new_york_coordinates, zoom_level=12)

This returns the following figure:

Easy first map using google maps api

Now, lets say that you would like to add earthquake data to your map. There is already a dataset that google provides, and you can use this very easily to add more information to your plot using the heatmap function.

import gmaps

import gmaps.datasets # Use google maps api

gmaps.configure(api_key=api_key) # Fill in with your API key # Get the dataset

earthquake_df = gmaps.datasets.load_dataset_as_df('earthquakes') #Get the locations from the data set

locations = earthquake_df[['latitude', 'longitude']] #Get the magnitude from the data

weights = earthquake_df['magnitude'] #Set up your map

fig = gmaps.figure()

fig.add_layer(gmaps.heatmap_layer(locations, weights=weights))

fig

Heatmap of Earthquake activity

This can also be used in the same way I used in my previous tutorial for fires around CA. Here is how the map looks when you add a layer of current fire activity:

Heatmap of current fire activity

Finally, you can get out driving directions from the api. To do this there are a couple of steps.

To plot route on map:

Define location 1 in coordinates Define location 2 in coordinates Create layer using gmaps.directions.Directions Add layer to the map

Code:

import gmaps

#configure api

gmaps.configure(api_key=api_key) #Define location 1 and 2

Durango = (37.2753,-107.880067)

SF = (37.7749,-122.419416) #Create the map

fig = gmaps.figure() #create the layer

layer = gmaps.directions.Directions(Durango, SF,mode='car') #Add the layer

fig.add_layer(layer)

fig

Driving Directions

And to get more information like the distance and travel time, you can use the gmaps client. The code for this is below:

Shows the duration and distance for the directions

There is a ton more that you can do with the api, and the documentation is awesome. If there is something I did not cover, it is a great resource in general!

https://media.readthedocs.org/pdf/jupyter-gmaps/latest/jupyter-gmaps.pdf