Draw time radius around lat/long on map

前端 未结 2 1232
梦毁少年i
梦毁少年i 2021-01-15 11:18

I am working with the gmapsdistance package in R. I have my API key, and I am familiar with the functions within the package.

However, I would like to w

相关标签:
2条回答
  • 2021-01-15 12:12

    As suggested in the comments, you can sign up to a service like Travel Time Platform (which I'm using in this example) and use their API to get the possible destinations given a starting point.

    Then you can plot this on a map using Google Maps (in my googleway package)

    appId <- "TravelTime_APP_ID"
    apiKey <- "TravelTime_API_KEY"
    mapKey <- "GOOGLE_MAPS_API_KEY"
    
    library(httr)
    library(googleway)
    library(jsonlite)
    
    location <- c(25.9840, -80.2821)
    driveTime <- 2 * 60 * 60
    
    ## London example
    ## location <- c(51.507609, -0.128315)
    
    ## sign up to http://www.traveltimeplatform.com/ and get an API key
    ## and use their 'Time Map' API 
    
    url <- "http://api.traveltimeapp.com/v4/time-map"
    
    requestBody <- paste0('{ 
    "departure_searches" : [ 
      {"id" : "test", 
      "coords": {"lat":', location[1], ', "lng":', location[2],' }, 
      "transportation" : {"type" : "driving"} ,
      "travel_time" : ', driveTime, ',
      "departure_time" : "2017-05-03T08:00:00z"
      } 
     ] 
    }')
    
    res <- httr::POST(url = url,
                         httr::add_headers('Content-Type' = 'application/json'),
                         httr::add_headers('Accept' = 'application/json'),
                         httr::add_headers('X-Application-Id' = appId),
                         httr::add_headers('X-Api-Key' = apiKey),
                         body = requestBody,
                         encode = "json")
    
    res <- jsonlite::fromJSON(as.character(res))
    
    pl <- lapply(res$results$shapes[[1]]$shell, function(x){
        googleway::encode_pl(lat = x[['lat']], lon = x[['lng']])
    })
    
    df <- data.frame(polyline = unlist(pl))
    
    df_marker <- data.frame(lat = location[1], lon = location[2])
    
    google_map(key = mapKey) %>%
        add_markers(data = df_marker) %>%
        add_polylines(data = df, polyline = "polyline") 
    

    0 讨论(0)
  • 2021-01-15 12:15

    If you want to render in leaflet and use a free isochrone service, this is a pretty neat option. There is a limit of 2 hours drive away though.

    devtools::install_github("tarakc02/rmapzen")
    library(rmapzen)
    Sys.setenv(MAPZEN_KEY = "") # get for free at https://mapzen.com/
    
    marimar <- mz_geocode("Marimar, FL")
    isos <- mz_isochrone(
      marimar,
      costing_model = mz_costing$auto(),
      contours = mz_contours(c(60 * 2))  # 2 hours 
    )
    
    library(leaflet)
    leaflet(as_sp(isos)) %>%
      addProviderTiles("CartoDB.DarkMatter") %>%
      addPolygons(color = ~paste0("#", color), weight = 1)
    
    0 讨论(0)
提交回复
热议问题