Map Your Google Location History

Step 1: Download your Google Location History

Google makes this process very easy. Go here to download your location history data and unzip it.

Step 2: Run this script

Preliminaries

In [11]:
# Import pandas
import pandas as pd

# Import matplotlib and Basemap
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

# Set iPython to display visualization inline
%matplotlib inline

Read in the location history json

Simply change the string to point to where you unzipped your location history json file

In [2]:
# Create a dataframe from the json file in the filepath
raw = pd.io.json.read_json('/Users/chrisralbon/Downloads/Location History/LocationHistory.json')

Let's take a look at some of the data

In [3]:
# View the last five rows of the dataframe
raw.tail()
Out[3]:
locations somePointsTruncated
369608 {'timestampMs': '1360251651345', 'longitudeE7'... True
369609 {'timestampMs': '1360251591250', 'longitudeE7'... True
369610 {'timestampMs': '1360251521053', 'longitudeE7'... True
369611 {'timestampMs': '1360249108976', 'longitudeE7'... True
369612 {'timestampMs': '1360242137146', 'longitudeE7'... True

Expand the locations object into it's own dataframe

In [4]:
# Expand the locations column into a dataframe
# This lets us move down one level in the json structure
df = raw['locations'].apply(pd.Series)

Take a peak at the data again

In [5]:
# View the last five rows of the dataframe
df.tail()
Out[5]:
accuracy activitys altitude heading latitudeE7 longitudeE7 timestampMs velocity
369608 57 NaN NaN NaN 388976217 -770434474 1360251651345 NaN
369609 52 NaN NaN NaN 388974040 -770433938 1360251591250 NaN
369610 34 NaN NaN NaN 388974020 -770432626 1360251521053 NaN
369611 50 NaN NaN NaN 388974020 -770432626 1360249108976 NaN
369612 1064 NaN NaN NaN 389467326 -769333633 1360242137146 NaN

Wrangle the data

In [6]:
# Create a list from the latitude column, multiplied by -E7
df['latitude'] = df['latitudeE7'] * 0.0000001

# Create a list from the longitude column, multiplied by -E7
df['longitude'] = df['longitudeE7'] * 0.0000001

Map the data using basemap

In [8]:
# Create a figure of size (i.e. pretty big)
fig = plt.figure(figsize=(20,10))

# Create a map, using the Gall–Peters projection, 
map = Basemap(projection='gall', 
              # with low resolution,
              resolution = 'l', 
              # And threshold 100000
              area_thresh = 100000.0,
              # Centered at 0,0 (i.e null island)
              lat_0=0, lon_0=0)

# Draw the coastlines on the map
map.drawcoastlines()

# Draw country borders on the map
map.drawcountries()

# Fill the land with grey
map.fillcontinents(color = '#888888')

# Draw the map boundaries
map.drawmapboundary(fill_color='#f4f4f4')

# Define our longitude and latitude points
x,y = map(df['longitude'].values, df['latitude'].values)

# Plot them using round markers of size 6
map.plot(x, y, 'ro', markersize=6)
 
# Show the map
plt.show()