This is based on the code Craig Eric Simpkins greatfully shared with me. But there is much more, here and here. #First get your data from https://takeout.google.com/. Select “Location History” in JSON format ONLY. Save it in an easier to use and quicker format for future use.
#Builds a heatmap using the data gathered by Google. Used code developed by https://www.cultureofinsight.com/blog/2018/01/31/2018-01-31-map-your-google-location-data-with-r-shiny/. Have simplified the code alot. Code also allows you to easily export images of the maps as .png
library(jsonlite)
library(leaflet)
library(leaflet.extras)
library(tidyverse)
library(lubridate)
library(colorRamps)
library(mapview)
#Import data and convert it to Rds - this can take a while
if (file.exists("Takeout/Standortverlauf/Standortverlauf.Rds")!=T){
d<-fromJSON("Takeout/Standortverlauf/Standortverlauf.json")
saveRDS(locationdata, file.path(dfolder,"Standortverlauf.Rds"))
}
d <- readRDS("Takeout/Standortverlauf/Standortverlauf.Rds")
#Select only the relevant columns
d <- d$locations %>%
select(latitudeE7, longitudeE7, `timestampMs`, velocity)
#Make data more viz friendly
d <- d %>%
mutate(lat = latitudeE7 / 1E7, lon = longitudeE7 / 1E7) %>%
mutate(timestampMs = as.numeric(timestampMs)) %>%
mutate(Date = as.POSIXct(timestampMs/1000, origin="1970-01-01"))
#Check the time frame
c(min(d$Date), max(d$Date))
## [1] "2013-08-29 17:11:08 CEST" "2018-10-28 09:52:21 CET"
#And filter to just the date range you want
i<-which(d$Date > as.Date("2016-08-15") & d$Date < as.Date("2019-01-28"))
length(i)
## [1] 81982
df<-d[i,]
c(min(df$Date), max(df$Date))
## [1] "2018-06-18 07:17:18 CEST" "2018-10-28 09:52:21 CET"
#Make a simple map
HeatGradient <- matlab.like2(5000)
m <- leaflet(df) %>%
setView(5.3810700, 43.2969500, 12) %>% #lon, lat, zoom #Need to pick your location
addProviderTiles(providers$Stamen.Terrain) %>%
addHeatmap(
lng = ~lon,
lat = ~lat,
blur =5,
max = 20,
radius = 6,
gradient = HeatGradient,
cellSize = 15
)
m
#Export
mapshot(m, file = "google-heatmap.png")