how to plot multiple isochrone on leaflet map in R

Viewed 1260

i can plot one isochrone in R at a time using Rmapzen package with this code, each isochrone is generated as a sp object, is there any way to create multiple isochrone on the same map like the attached picenter image description here?

2 Answers

This works for me. A bit messy though.. Remember to change the mapzen key

library(rmapzen)
library(leaflet)
library(ggmap)
#packages

Sys.setenv(MAPZEN_KEY = "mapzen-******")
#API key

ucb <- geocode("Via Giovanni Spadolini 7, Milan, Italy")
ucb1 <- geocode("Via Valtellina 15, Milan, Italy")
#origin address

iso5 <- as_sp(mz_isochrone(
  ucb,
  costing_model = mz_costing$auto(),
  contours = mz_contours(5),
  polygons = TRUE
))
iso15 <- as_sp(mz_isochrone(
  ucb,
  costing_model = mz_costing$auto(),
  contours = mz_contours(15),
  polygons = TRUE
))
iso1_5 <- as_sp(mz_isochrone(
  ucb1,
  costing_model = mz_costing$auto(),
  contours = mz_contours(5),
  polygons = TRUE
))
iso1_15 <- as_sp(mz_isochrone(
  ucb1,
  costing_model = mz_costing$auto(),
  contours = mz_contours(15),
  polygons = TRUE
))

m = leaflet() %>%
  addProviderTiles("CartoDB.DarkMatter") %>%
  addPolygons(data = iso15, color = "green", fillColor = "green", fillOpacity = .5)%>%
  addPolygons(data = iso5, color = "blue", fillColor = "blue", fillOpacity = .5)%>%
  addPolygons(data = iso1_15, color = "green", fillColor = "green", fillOpacity = .5)%>%
  addPolygons(data = iso1_5, color = "blue", fillColor = "blue", fillOpacity = .5)
m
Related