【发布时间】:2011-03-16 12:09:17
【问题描述】:
我已尝试通过 Google 地图和此博客文章中的 XML 包运行代码以对 R 中的位置进行地理编码:
http://www.r-chart.com/2010/07/maps-geocoding-and-r-user-conference.html
这是他的功能:
getDocNodeVal=function(doc, path){
sapply(getNodeSet(doc, path), function(el) xmlValue(el))
}
gGeoCode=function(str){
library(XML)
u=paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
doc = xmlTreeParse(u, useInternal=TRUE)
str=gsub(' ','%20',str)
lng=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lat")
lat=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lng")
c(lat,lng)
}
当我运行gGeoCode() 时,我收到以下错误:
> gGeoCode("Philadelphia, PA")
failed to load external entity "http%3A//maps.google.com/maps/api/geocode/xml%3Fsensor=false&address=%20Philadelphia,%20PA"
Error: 1: failed to load external entity "http%3A//maps.google.com/maps/api/geocode/xml%3Fsensor=false&address=%20Philadelphia,%20PA"
如果我只是将带有 Philadelphia, PA 的 API url 粘贴到浏览器中,就像传递给 xmlParseTree 的字符串一样,当我下载它时,我会得到一个看起来像合法 xml 的结果。
这是代码的问题,还是我未能配置某些东西?
【问题讨论】:
-
旁白/题外话:他可以用 str=URLencode(str) 代替 str=gsub(' ','%20',str)。我只在这里评论是因为我认为这是一个很酷的功能:)
-
是的,一个很酷的功能。如果它起作用了! :-/
-
json 调用有效,请参阅下面的答案 :)
-
我知道这个问题很古老,但值得补充的是,
ggmap包中现在有一个geocode函数可以为您完成所有这些工作。
标签: google-maps r geocoding