街道地址转地理位置纬度/经度

12

我正在考虑使用rChart/LeafLet为我的县创建一个晶亮应用程序,与住房销售相关。每次都有几百所房屋出售。希望将所有的街道地址映射到地理位置(纬度/经度),并在地图上显示它们。因此,我正在寻找一个能够将街道地址映射到地理位置的r软件包、服务或数据库。


6
nominatim: https://github.com/hrbrmstr/nominatim; ggmap::geocode; geocodeHERE::geocodeHERE_simple; geonames包; 还有 google r 街道地址地理定位注:以上内容为需要翻译的内容,下同。 - hrbrmstr
优秀的文章,带有代码示例(使用ggmap包):http://www.shanelynn.ie/massive-geocoding-with-r-and-google-maps/ - Paulo S. Abreu
3个回答

24

这是一个基于哈维建议的函数。它将查找地址并给出第一个结果的坐标。查看函数中x的结构,以了解其他可以获取的信息。

geocodeAdddress <- function(address) {
  require(RJSONIO)
  url <- "http://maps.google.com/maps/api/geocode/json?address="
  url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
  x <- fromJSON(url, simplify = FALSE)
  if (x$status == "OK") {
    out <- c(x$results[[1]]$geometry$location$lng,
             x$results[[1]]$geometry$location$lat)
  } else {
    out <- NA
  }
  Sys.sleep(0.2)  # API only allows 5 requests per second
  out
}

例如:

R> geocodeAdddress("Time Square, New York City")
[1] -73.98722  40.7575

为什么我们要构建函数,而存在完全相同功能的包? - hrbrmstr
@hrbrmstr,我之前不知道那个包,谢谢你提醒我。 - maccruiskeen
你能分享一下 choff 的包名吗? - Antex
这是hrbrmstr在问题评论中提到的那个,nominatim。 - maccruiskeen
感谢 @choff 提供的函数! - user5363218

5

3
以后参考,请将此留在评论中,但我猜您的声望不够,无法这样做,所以好吧。 - jlhoward
1
谢谢Harvey。Google Maps限制每秒请求次数为10次或每天2500次,这很好。是否有一种批量请求的方法?比如我发送100个街道地址并获得100个经纬度? - Antex
抱歉,我应该提到有一个限制。但对于小型应用程序来说,这并不是什么大问题。我成功地在iOS应用程序中通过简单地循环请求执行了多个操作。尽管由于多个连接过快可能会锁定您的帐户,因此您可能需要限制其速度。抱歉,我不知道R语言的具体细节! - Harvey

1

针对API要求进行修改的答案:

api_key <- c("YOUR_API_KEY")

geocodeAdddress <- function(address) {
  require(RJSONIO)
  url <- "https://maps.googleapis.com/maps/api/geocode/json?address="
  url <- URLencode(paste(url, address, sep = ""))
  url <- URLencode(paste(url, "&key=", api_key, sep = ""))
  x <- fromJSON(url, simplify = FALSE)
  print(x$status)
  if (x$status == "OK") {
    out <- c(x$results[[1]]$geometry$location$lng,
             x$results[[1]]$geometry$location$lat)
  } else {
    out <- NA
  }
  Sys.sleep(0.2)  # API only allows 5 requests per second
  out
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接