在地图上绘制纬度/经度的时间半径

时间:2016-11-08 14:14:15

标签: r google-maps travel-time

我正在使用R中的gmapsdistance包。我有我的API密钥,我熟悉包中的功能。

但是,我想反过来解决问题。而不是仅仅在lat / long之间找到TimeDistanceStatus是lat / long的向量,我想输入lat / long,并绘制一个区域可以在3小时或更短的时间内开出的积分。然后我想在Google地图上绘制它。 首先,使用Marimar,FL:25.9840,-80.2821会很棒。

有没有人有这类问题的经验?

2 个答案:

答案 0 :(得分:4)

正如评论中所建议的那样,您可以注册Travel Time Platform这样的服务(我在此示例中使用了该服务),并使用他们的API获取可能的目的地。

然后,您可以使用Google地图(在我的googleway套餐中)将其绘制在地图上

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") 

enter image description here

答案 1 :(得分:2)

如果你想在传单中渲染并使用免费的等时服务,这是一个非常简洁的选择。但是距离酒店有2个小时的车程。

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)