Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

title thumbnailImagePosition thumbnailImage coverImage metaAlignment coverMeta date categories tags
A beautiful day in the neighborhood: Google location history
2018-03-15 21:13:14 -0500
Personal projects
Google location history
knitr::opts_chunk$set(echo = TRUE)

Since moving to San Francisco at the end of February, I have been driving and walking around town, trying to get to know my new city. And even though I've only been here a few weeks, I already feel affinity1 for my own neighborhood. Which is good, because I'm also too lazy to leave it.

I also learned recently that you can download your location history via google. In the spirit of discovering my new city and possibly also discovering something about my (homebody) self, today I am going to make a map of where I have been since moving here three weeks ago.

Google location history

To get access to your location history, you simply go to [] and select location history to download.

The location history is a json file, and to open it you will need an additional R package like rjson.

# install.packages("rjson")

Once you've loaded the package, you can use the command fromJSON to open the file. Since Google keeps an amazing amount of information about all of us, this takes a while.

json_file <- "Takeout 2/Location History/Location History.json"
json_data <- fromJSON(file=json_file)

The data is stored in the form of json_data$locations with an additional [[1]] - [[N]] for each location google recorded. Each location has a varying number of additional information in the form of a list, including:

  • timestampMs
  • lattitudeE7
  • longituteE7
  • accuracy
  • velocity
  • heading
  • altitude
  • vertical accuracy
  • activity

Some of these names are obvious and some are totally enigmatic, and even when it's obvious from the name what is being measured, it's not always obvious what the unit is or what to do with that information. For the time being, I'm just going to deal with the first three: the time stamp, lattitude, and longitude to make a map of where I've been. I start by extracting the information I need from the json data into a dataframe of the type I'm used to working with

Time stamps

The Ms at the end of the timestamp name indicates that it is is in milliseconds from Jan 1, 1970. We can easily convert dates in this format with the R command "as.POSIXct".

## convert dates for 2.5 weeks (that's the most recent 7600 records!)
# extract dates
timestamp <- c()
for (i in 1:7600){
timestamp[i] <- as.POSIXct(as.numeric(json_data$locations[[i]]$timestampMs)/1000, origin = "1970-01-01")


Now we extract lattitude and longitude

# extract lattitude
lat <- c()
for (i in 1:7600){
  lat[[i]] <- json_data$locations[[i]]$latitudeE7 / 1e7

# longitude
lon <- c()
for (i in 1:7600){
  lon[[i]] <- json_data$locations[[i]]$longitudeE7 / 1e7

Data frame

And create one dataframe with time stamp, lattitude, and longitude

# create df
locationdf <-, lat, lon))


Now that we have a data frame with the date, lattitude, and longitude of each google location, we can start the visualizaiton.

Load the map

We first load our map of san francisco into R, using the R package ggmap. Note: I kept getting an error when trying to run qmap until I installed the devtools version of ggmap using this line: devtools::install_github("dkahle/ggmap"). Just FYI in case you run into the same problem!

# install packages

# load package

## load map
sf <- qmap(location = "noe valley - san francisco", maptype = "roadmap", zoom = 14, color = "bw")

In the above I made the zoom 14 after playing around with it a bit, and I made the map in black and white (color = "bw") so my routes will stand out more clearly.

Overlaying routes

Now it's time to overlay routes.

sf + geom_point(data = locationdf, aes(x = locationdf$lon, y = locationdf$lat), alpha = 0.5, color = "red") 

As the map shows, I have mostly stayed within one neighborhood, with a few trips to the west and north/northeast. Just for scale, I'm now going to repeat the map with a wider zoom so you can see how this looks compared to San Francisco overall.

sfzoom <- qmap(location = "noe valley - san francisco", maptype = "roadmap", zoom = 12, color = "bw")
sfzoom + geom_point(data = locationdf, aes(x = locationdf$lon, y = locationdf$lat), alpha = 0.5, color = "red") 

I ended up not doing anything with time stamp in this analysis, but it would be interesting for future projects to look at where I am at certain times of day and try to assess "home" or "work" based on the hours that I'm there. You could also use the time stamp to distinguish routes from destinations, for example.

And although it's super easy to access and visualize your location history yourself using the R code above, google has a dashboard to make this process even easier: []. Very interesting and worth checking out! Also, if you're looking for more examples of how to use google location history data, there have been a few excellent tutorials on this by Shirin's playgRound and Visual Cinnamon.

This blog post can be found on GitHub.

Well, I was feeling affinity for SF before I got my second parking ticket in two weeks!

You can’t perform that action at this time.