2017-09-15 111 views
0

我想在河包ggmap與功能geocode工作,但我不能運行R文件中的代碼示例:錯誤fromJSON:意外的字符

geocode("houston texas", output = "more") 

,其結果是:

Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=houston%20texas&sensor=false 
Error in fromJSON(paste(lines, collapse = "")) : unexpected character '<' 

我不知道它是與fromJSON的問題,所以我下載了JSON文件(如abcd.json),然後將其導入R:

data <- fromJSON(file="abcd.json") 

它的工作原理。

但是當我做:

theurl <- "http://maps.googleapis.com/maps/api/geocode/json?address=houston%20texas&sensor=false" 
data <- fromJSON(file=theurl) 

它返回相同的錯誤:

Error in fromJSON(paste(lines, collapse = "")) : unexpected character '<' 

Rcurl都嘗試rjsonRJSONIO在一起。它以任何方式返回錯誤。


相關信息:

  1. RStudio版本1.0.143;
  2. R版本3.4.0
  3. macOS Sierra 10.12.6
  4. 位置:北京。 (可能有一些做互聯網禁止谷歌地圖API的。)
+0

和你的'geocode()'函數對我來說不會出錯。也許你可以試試'library(googleway); google_geocode(「houston texas」,key =「」)' – SymbolixAU

+0

@SymbolixAU他們使用'rjson'實現,它使用'file'而不是'text':https://www.rdocumentation.org/packages/ rjson/versions/0.2.15/topics/fromJSON – miken32

+0

@ miken32 - 啊是 - 好點。 – SymbolixAU

回答

0

鑑於無效字符是一個<,你很可能會拉動一些類型的HTML錯誤消息。直接把數據URL會讓你肯定知道:

theurl <- "http://maps.googleapis.com/maps/api/geocode/json?address=houston%20texas&sensor=false" 
jsondata <- fread(theurl) 
cat(jsondata) 

我建議您嘗試使用HTTPS連接,而不是,它可能是會得到解決的問題

0

(除非它是偉大的防火牆。)我選擇使用工具箱JSONlab轉向Matlab。

http://cn.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files

的代碼的其餘部分的總體思路是幾乎一樣geocode()

和它的工作。

對於那些位於中國大陸的人員/機構,R包ggmap中的很多功能不可用(由於防火牆原因)。使用軟件包geoChina來代替您的項目。百度API可在geoChina。在中國這比谷歌地圖API更快。

另外,如果你的數據是相當大的,

result <- apply(data, 1, geocode) 

可能會返回錯誤。建議使用Matlab來處理相對較大的數據。