增加ggmap中geocode函数的API限制(在R中)

15

我试图在R中使用ggmaps库中的geocode函数来获取特定位置的坐标。到目前为止,我能够很好地使用该函数。

我遇到的问题是,我想将我的每日限制从2,500增加到100,000。谷歌官方文档表示,如果您在项目上启用计费,这是完全可能的,而我也很乐意这样做。当您继续进行此过程时,Google Developers Console将为您提供个性化的API密钥。

然而,geocode函数没有选项可以输入此个性化的API密钥。相反,它要求输入client(业务用户的客户端ID)和signature(业务用户的签名),这是Google Maps API for Work客户可以访问API的方式。我知道这也是一个选项,但是这似乎是非常特殊的情况,因为Google Maps API for Work似乎是专为大型企业账户设计的:

根据年度购买合同,每日限额开始为每24小时的100,000个请求。

因此,我的问题归结为:我能否使用R中的ggmaps库中的geocode函数来访问Google Maps Geocoding API?

4个回答

14

我编写了一个名为googleway的包,它可以访问Google Maps API,您可以在其中指定自己的API密钥。

例如:

library(googleway)

key <- "your_api_key"

google_geocode(address = "San Francisco",
               key = key)

# $results
# address_components
# 1 San Francisco, San Francisco County, California, United States, SF, San Francisco County, CA, US, locality, political, administrative_area_level_2, political, administrative_area_level_1, political, country, political
# formatted_address geometry.bounds.northeast.lat geometry.bounds.northeast.lng geometry.bounds.southwest.lat
# 1 San Francisco, CA, USA                      37.92977                     -122.3279                      37.69313
# geometry.bounds.southwest.lng geometry.location.lat geometry.location.lng geometry.location_type
# 1                     -123.1661              37.77493             -122.4194            APPROXIMATE
# geometry.viewport.northeast.lat geometry.viewport.northeast.lng geometry.viewport.southwest.lat
# 1                          37.812                       -122.3482                         37.7034
# geometry.viewport.southwest.lng                    place_id               types
# 1                        -122.527 ChIJIQBpAG2ahYAR_6128GcTUEo locality, political
# 
# $status
# [1] "OK"

14

使用 ggmap 版本 2.7 或更高版本(截至 12 月 13 日,尚未在 CRAN 上可用,但您可以使用 devtools::install_github("dkahle/ggmap") 进行安装),您只需要运行 register_google(key = 'LONG KEY STRING'),然后您就可以调用任何 ggmap 函数,例如 geocodemutate_geocode 并使用您的 API 密钥。


6
现在是2018年3月,2.7版本仍未进入CRAN。;( - yuk
@yuk 现在是2018年10月,仍然没有进入CRAN,现在geocode完全无法使用,没有API密钥:Keyless access to Google Maps Platform is deprecated. Please use an API key with all your API calls to avoid service interruption. For further details please refer to http://g.co/dev/maps-no-account - Arthur

6

谢谢你!这对我帮助很大。

虽然你的解决方案相当具体,但我想包括我对你的函数所做的适应。它出现了错误,因为raw_datageo_data_list未定义。我猜这些是特定于你的本地环境的。

对于我来说,输入位置并返回纬度和经度可以使用以下代码:

 getGeoData <- function(location, api_key){
  location <- gsub(' ','+',location)
  geo_data <- getURL(paste("https://maps.googleapis.com/maps/api/geocode/json?address=",location,sprintf("&key=%s",api_key), sep=""))
  geo_data <- fromJSON(geo_data)
  return(geo_data$results[[1]]$geometry$location)
}

您可以修改返回语句以索引geo_data以获取除纬度和经度之外的其他属性。

希望这对某些人有所帮助。

R


5
我并没有找到一个使用现有的ggmap库中的geocode函数来回答这个问题的方法,所以我自己创建了一个新的函数,仅仅使用现有的RCurl库中的getURL函数和RJSONIO库中的fromJSON函数来完成。请看下面的新函数代码:
library(RJSONIO)
library(RCurl)

getGeoData <- function(location){
  location <- gsub(' ','+',location)
  geo_data <- getURL(paste("https://maps.googleapis.com/maps/api/geocode/json?address=",location,"&key=**[YOUR GOOGLE API KEY HERE]**", sep=""))
  raw_data_2 <- fromJSON(geo_data)
  return(raw_data_2)
}

测试: getGeoData("旧金山")

这将给你一个列表,其中包含与geocode("旧金山")产生的列表几乎相同但不完全相同格式的相同数据。


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