我正在遵循这个问题中提供的答案:在 R 中使用传单绘制旅程路径
并希望复制相同的想法:绘制一张显示城市之间驾驶路线的地图。
作为参考,当我简单地复制并粘贴 SymbolixAu 答案中的代码时......效果很好! (简单的地图,即 googe_map,而不是“闪亮”的代码)。
换句话说,我认为我的 api 密钥设置得很好。但由于某种原因,当我尝试在位置数据上使用相同的代码时,它不起作用。这是我的代码:
df_locations<-structure(list(origin = c("WARWICK", "EAST PROVIDENCE", "WARREN",
"CENTERDALE", "CENTRAL FALLS", "DAVISVILLE", "NORTH PROVIDENCE",
"EAST PROVIDENCE", "PROVIDENCE", "CHEPACHET"), destination = c("CENTERDALE", "EAST PROVIDENCE", "BRISTOL", "JOHNSTON", "CRANSTON", "WARWICK","NORTH PROVIDENCE", "EAST PROVIDENCE", "WARREN", "CHEPACHET")), class = "data.frame", row.names = c(NA, -10L))
## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
res <- google_directions(
key = api_key
, origin = x[['origin']]
, destination = x[['destination']]
)
df_result <- data.frame(
origin = x[['origin']]
, destination = x[['destination']]
, route = res$routes$overview_polyline$points
)
return(df_result)
})
## convert the results to a data.frame
df_directions <- do.call(rbind, lst_directions)
## plot the map
google_map(key = map_key ) %>%
add_polylines(data = df_directions, polyline = "route")
当我进入“apply”循环时,我收到错误“data.frame(origin = x[["origin"]],destination = x[["destination"]], 中的错误: 参数意味着不同的行数:1, 0"
当我重新运行完全相同的代码,但仅使用示例数据框时:
df_locations <- data.frame(
origin = c("Melbourne, Australia", "Sydney, Australia")
, destination = c("Sydney, Australia", "Brisbane, Australia")
, stringsAsFactors = F
)
效果很好。有任何想法吗?我想知道是否因为我的位置只有城市名称(没有州或国家),所以我将示例数据调整为仅说“墨尔本”或“悉尼”,仍然有效。
不管怎样,我只是运行了你的代码,没有任何问题。因此,@Dave2e 怀疑您的 API 调用可能有问题,要么是超时,要么是 google 由于某种原因没有返回结果。因此,您必须继续调试/打印结果,看看是否是这种情况。
df_locations<-structure(list(origin = c("WARWICK", "EAST PROVIDENCE", "WARREN",
"CENTERDALE", "CENTRAL FALLS", "DAVISVILLE", "NORTH PROVIDENCE",
"EAST PROVIDENCE", "PROVIDENCE", "CHEPACHET"), destination = c("CENTERDALE", "EAST PROVIDENCE", "BRISTOL", "JOHNSTON", "CRANSTON", "WARWICK","NORTH PROVIDENCE", "EAST PROVIDENCE", "WARREN", "CHEPACHET")), class = "data.frame", row.names = c(NA, -10L))
library(googleway)
set_key("MYKEY")
## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
res <- google_directions(
origin = x[['origin']]
, destination = x[['destination']]
)
df_result <- data.frame(
origin = x[['origin']]
, destination = x[['destination']]
, route = res$routes$overview_polyline$points
)
return(df_result)
})
df_directions <- do.call(rbind, lst_directions)
## plot the map
google_map() %>%
add_polylines(data = df_directions, polyline = "route")
如果将函数分成两部分,则更容易看到错误。
lst_directions <- apply(df_locations, 1, function(x){
res <- google_directions(
origin = x[['origin']],
destination = x[['destination']]
)
})
结果会创建一个列表,您可以看到哪些地理编码失败。例如,当我运行时,出现以下错误。
lst_directions[[1]]
$geocoded_waypoints
geocoder_status place_id types
1 ZERO_RESULTS <NA> NULL
2 OK ChIJfbgEZoJG5IkRezxu2nfHggk locality, political
$routes
list()
$status
[1] "NOT_FOUND"
有效的输出如下
lst_directions[[2]]
$geocoded_waypoints
geocoder_status place_id types
1 OK ChIJNYT8S5ha5IkRgDDkgjwJHgc locality, political
2 OK ChIJNYT8S5ha5IkRgDDkgjwJHgc locality, political
$routes
bounds.northeast.lat bounds.northeast.lng bounds.southwest.lat bounds.southwest.lng copyrights
1 41.81361 -71.37006 41.81361 -71.37006 Map data ©2024 Google
legs
1 1 m, 0, 1 min, 0, East Providence, RI, USA, 41.8136127, -71.3700603, East Providence, RI, USA, 41.8136127, -71.3700603, 1 m, 0, 1 min, 0, 41.8136127, -71.3700603, Head on <b>Warren Ave</b>, ave~FzmrrL, 41.8136127, -71.3700603, DRIVING
points summary warnings waypoint_order
1 ave~FzmrrL Warren Ave NULL NULL
$status
[1] "OK"
仍然可能是建议的地理编码问题,但至少这有助于调试。