我正在尝试对一个大型数据集进行反向地理编码。我正在使用RJSONIO包和Google map API来获取给定经度在数据集中的位置。在100或150成功显示位置信息后,它将显示:
Warning message - "In readLines(con) : cannot open: HTTP status was '0 (null)'"和:
Error : "Error in fromJSON(paste(readLines(con), collapse = "")) :
error in evaluating the argument 'content' in selecting a method for function 'fromJSON': Error in readLines(con) : cannot open the connection"
location<-function(latlng){
latlngStr <- gsub(' ','%20', paste(latlng, collapse=","))
library("RJSONIO") #Load Library
#Open Connection
connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=',latlngStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
data.json <- unlist(data.json)
if(data.json["status"]=="OK")
address <- data.json["results.formatted_address"]
print (address)
} 可能的原因是什么以及如何解决该问题?
我使用的是R版本3.2.1和Ubuntu 14.10。
发布于 2016-06-29 04:11:47
最有可能的是你达到了使用限制,总是有限制的:https://developers.google.com/maps/documentation/geocoding/usage-limits
除此之外,您可以(合法地)使用API响应的功能也存在一些限制,特别是在存储这些响应方面:https://developers.google.com/maps/documentation/geocoding/policies https://developers.google.com/maps/terms#10-license-restrictions (特别参见10.5.d )。
发布于 2018-04-07 08:34:06
我在使用ggmap函数访问google API时也遇到过类似的错误。如果您处于速率限制之下,则可能是服务器无法响应(任何服务器有时都会这样做,并且您拉取的数据越多,您就越有可能遇到这种情况)。
如果您的代码中没有任何错误处理,那么当服务器没有响应时,您的脚本将中断并显示一条错误消息。简单的解决方案是内置一些错误处理,如果API返回错误(例如,如果不起作用,再试一次),就重新pinging几次。
以下是在我的脚本中修复它的方法:
attempt = 1 #start attempt counter
while(attempt != 20) #repeat the API request for up to 20 times
{
#use try to test for whether or not your API function returns error
dat.test <- try(PASTE YOUR FUNCTION THAT IS CALLING THE API INTO HERE)
if(is(dat.test, 'try-error')) #check if try returned error when pinging the API
{
#do these things if an error is returned
#if there is an error, after completing these items the while loop will continue to the next attempt to reach the API
Sys.sleep(1) #wait 1 second
warning("reattempting google api fetch...") #warn the user
attempt <- attempt + 1 #update the attempt counter
} else break #exit the while loop if no error returned (API returned data successfully))
}https://stackoverflow.com/questions/31667985
复制相似问题