Get your tracks from the Strava API and plot them on Leaflet maps

May 1, 2018

[This article was first published on Rcrastinate, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here is some updated R code from my previous post. It doesn’t throw any warnings when importing tracks with and without heart rate information. Also, it is easier to distinguish types of tracks now (e.g., when you want to plot runs and rides separately). Another thing I changed: You get very basic information on the track when you click on it (currently the name of the track and the total length).

Have fun and leave a comment if you have any questions.

options(stringsAsFactors = F)



token <- “

# Functions ————————————————————— <- function (stream.obj) {
  data.frame(lat = sapply(stream.obj[[1]]$data, USE.NAMES = F, FUN = function (x) x[[1]]),
             lon = sapply(stream.obj[[1]]$data, USE.NAMES = F, FUN = function (x) x[[2]]))
} <- function (, token) {
  stream <- GET(“”,
                path = paste0(“api/v3/activities/”,, “/streams/latlng”),
                query = list(access_token = token))

get.activities2 <- function (token) {
  activities <- GET(“”, path = “api/v3/activities”,
                    query = list(access_token = token, per_page = 200))
  activities <- content(activities, “text”)
  activities <- fromJSON(activities)
  res.df <- data.frame()
  for (a in activities) {
    values <- sapply(c(“name”, “distance”, “moving_time”, “elapsed_time”, “total_elevation_gain”,
                       “type”, “id”, “start_date_local”,
                       “location_country”, “average_speed”, “max_speed”, “has_heartrate”, “elev_high”,
                       “elev_low”, “average_heartrate”, “max_heartrate”), FUN = function (x) {
                         if (is.null(a[[x]])) {
                           NA } else { a[[x]] }
    res.df <- rbind(res.df, values)
  names(res.df) <- c(“name”, “distance”, “moving_time”, “elapsed_time”, “total_elevation_gain”,
                     “type”, “id”, “start_date_local”,
                     “location_country”, “average_speed”, “max_speed”, “has_heartrate”, “elev_high”,
                     “elev_low”, “average_heartrate”, “max_heartrate”)

get.multiple.streams <- function (act.ids, token) {
  res.list <- list()
  for ( in 1:length(act.ids)) {
    if ( %% 5 == 0) cat(“Actitivy no.”,, “of”, length(act.ids), “\n”)
    stream <-[], token)
    coord.df <-
    res.list[[length(res.list) + 1]] <- list( = act.ids[],
                                             coords = coord.df)

activities <- get.activities2(token)

stream.list <- get.multiple.streams(activities$id, token)

# Leaflet —————————————————————–

lons.range <- c(9.156572, 9.237580)
lats.range <- c(48.74085, 48.82079)

map <- leaflet() %>%
  addProviderTiles(“OpenMapSurfer.Grayscale”, # nice: CartoDB.Positron, OpenMapSurfer.Grayscale, CartoDB.DarkMatterNoLabels
                   options = providerTileOptions(noWrap = T)) %>%
  fitBounds(lng1 = min(lons.range), lat1 = max(lats.range), lng2 <- max(lons.range), lat2 = min(lats.range)) <- function (, color,, act.dist, strlist = stream.list) {
  act.ind <- sapply(stream.list, USE.NAMES = F, FUN = function (x) {
    x$ ==
  act.from.list <- strlist[act.ind][[1]]
  map <<- addPolylines(map, lng = act.from.list$coords$lon,
               lat = act.from.list$coords$lat,
               color = color, opacity = 1/3, weight = 2,
               popup = paste0(, “, “, round(as.numeric(act.dist) / 1000, 2), ” km”))

# plot all
for (i in 1:nrow(activities)) {[i, “id”], ifelse(activities[i, “type”] == “Run”, “red”,
                                      ifelse(activities[i, “type”] == “Ride”, “blue”, “black”)),
          activities[i, “name”], activities[i, “distance”])


To leave a comment for the author, please follow the link and comment on their blog: Rcrastinate. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)