Google 地图 api 未找到状态 R
Google maps api status NOT FOUND R
我创建了访问 Google 地图 API 的函数。我想知道从不同点到目标位置需要多长时间。
getDuration <- function(from,to,tMode,key){
from <- iconv(from, to="UTF-8")
to <- iconv(to, to="UTF-8")
tMode <- iconv(tMode, to="UTF-8")
from <- URLencode(from)
to <- URLencode(to)
tMode <- URLencode(tMode)
strQuery <- paste0(
"https://maps.googleapis.com/maps/api/directions/json?",
paste0("origin=",from),
paste0("&","destination=",to),
paste0("&","mode=",tMode),
paste0("&key=",key)
)
print(strQuery)
jDist <- fromJSON(strQuery,simplifyDataFrame = T)
if (jDist$status != "OK"){
print(paste0("Bad status: ",jDist$status))
return(NA)
}
if (length(jDist$routes)==0){
print("no route")
return(NA)
}
if (length(jDist$routes$legs)==0){
print("no legs")
return(NA)
}
return(jDist$routes$legs[[1]]$duration$value)
}
然后我尝试将此函数应用于字符向量 "from":
from
[1] “Étampes” “Étréchy” "Dourdan" "La Ferté-Alais" "Méréville" "Saint-Chéron"
sapply(from,function(x) { getDuration(x,to,"driving",key) })
我得到的输出如下:
Étampes Étréchy Dourdan La Ferté-Alais Méréville Saint-Chéron
NA NA 3501 4280 NA NA
这很奇怪,因为 Étampes 和我的目标目的地之间的路线存在并且它不是空的:
https://maps.googleapis.com/maps/api/directions/json?origin=%C3%83%E2%80%B0tampes&destination=Cours%20Valmy,%20Nanterre&mode=driving&key=AIzaSyBrmNaCXH_ppK7F0uW4SXZhPIBoDLQdKFE
有人知道如何找出问题的根源吗?
如果你使用googleway
,这个问题似乎不存在
library(googleway)
set_key("GOOGLE_API_KEY")
res <- google_directions(
origin = "Étampes",
destination = "cours valmy"
)
direction_legs(res)$distance
# text value
# 1 62.6 km 62648
direction_legs(res)$duration
# text value
# 1 1 hour 8 mins 4065
我创建了访问 Google 地图 API 的函数。我想知道从不同点到目标位置需要多长时间。
getDuration <- function(from,to,tMode,key){
from <- iconv(from, to="UTF-8")
to <- iconv(to, to="UTF-8")
tMode <- iconv(tMode, to="UTF-8")
from <- URLencode(from)
to <- URLencode(to)
tMode <- URLencode(tMode)
strQuery <- paste0(
"https://maps.googleapis.com/maps/api/directions/json?",
paste0("origin=",from),
paste0("&","destination=",to),
paste0("&","mode=",tMode),
paste0("&key=",key)
)
print(strQuery)
jDist <- fromJSON(strQuery,simplifyDataFrame = T)
if (jDist$status != "OK"){
print(paste0("Bad status: ",jDist$status))
return(NA)
}
if (length(jDist$routes)==0){
print("no route")
return(NA)
}
if (length(jDist$routes$legs)==0){
print("no legs")
return(NA)
}
return(jDist$routes$legs[[1]]$duration$value)
}
然后我尝试将此函数应用于字符向量 "from":
from
[1] “Étampes” “Étréchy” "Dourdan" "La Ferté-Alais" "Méréville" "Saint-Chéron"
sapply(from,function(x) { getDuration(x,to,"driving",key) })
我得到的输出如下:
Étampes Étréchy Dourdan La Ferté-Alais Méréville Saint-Chéron
NA NA 3501 4280 NA NA
这很奇怪,因为 Étampes 和我的目标目的地之间的路线存在并且它不是空的: https://maps.googleapis.com/maps/api/directions/json?origin=%C3%83%E2%80%B0tampes&destination=Cours%20Valmy,%20Nanterre&mode=driving&key=AIzaSyBrmNaCXH_ppK7F0uW4SXZhPIBoDLQdKFE
有人知道如何找出问题的根源吗?
如果你使用googleway
library(googleway)
set_key("GOOGLE_API_KEY")
res <- google_directions(
origin = "Étampes",
destination = "cours valmy"
)
direction_legs(res)$distance
# text value
# 1 62.6 km 62648
direction_legs(res)$duration
# text value
# 1 1 hour 8 mins 4065