增加ggmap地理编码功能中的api限制(在R中)

时间:2021-05-14 15:48:20

I'm trying to use the geocode function from the ggmaps library in R to get coordinates for specific locations. I'm able to use the function fine so far.

我正在尝试使用R中ggmaps库中的地理编码功能来获取特定位置的坐标。到目前为止,我能够正常使用该功能。

The issue I'm running into is that I would like to increase my daily limit from 2,500 to 100,000. The official Google documentation says that this is readily possible if you enable billing on the project, which I'm happy to do. When you proceed with this process, the Google Developers Console gives you a personalized API key.

我遇到的问题是我希望将每日限额从2,500增加到100,000。官方Google文档说,如果您在项目上启用结算,这很容易实现,我很乐意这样做。继续此过程后,Google Developers Console会为您提供个性化的API密钥。

However, the geocode function doesn't have an option to put in this personalized API key. Instead, it asks for the client (client ID for business users) and signature(signature for business users), which is how Google Maps API for Work customers can access the API. I get that this is also an option, but that seems to be a very use case, since Google Maps API for Work seems to be designed for large enterprise accounts:

但是,地理编码功能无法选择放入此个性化API密钥。相反,它要求客户端(业务用户的客户端ID)和签名(业务用户的签名),这就是Google Maps API for Work客户可以访问API的方式。我知道这也是一个选项,但这似乎是一个非常大的用例,因为Google Maps API for Work似乎是为大型企业帐户设计的:

Daily quota starting at 100,000 requests per 24 hours, based on annual contractual purchase.

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

So my question boils down to this: can I use the geocode function from the ggmapslibrary in R to ping the Google Maps Geocoding API?

所以我的问题归结为:我可以使用R中ggmapslibrary的地理编码功能ping Google Maps Geocoding API吗?

4 个解决方案

#1


12  

With ggmap version 2.7 or greater (as of 13 Dec, not yet available on Cran, but you can install with devtools::install_github("dkahle/ggmap"), you simply need to run register_google(key = 'LONG KEY STRING') and then you can call any of the ggmap functions such as geocode or mutate_geocode and use your API key.

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

#2


12  

I've written the package googleway to access google maps API where you can specify your api key.

我已经编写了googleway软件包来访问谷歌地图API,您可以在其中指定您的API密钥。

For example

例如

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"

#3


5  

Thanks for this! It helped me along immensely. Your solution is pretty specific, so I wanted to include the adaptations I made of your function. It threw bugs because raw_data and geo_data_list are undefined. I'm guessing these were specific to your local environment.

谢谢你!它极大地帮助了我。您的解决方案非常具体,因此我希望包含我对您的功能进行的调整。它抛出了错误,因为raw_data和geo_data_list未定义。我猜这些是特定于您当地的环境。

For me, inputting a location and returning the lat, lon worked with this:

对我来说,输入一个位置并返回lat,lon使用了这个:

 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)
}

You can modify the return statement to index into geo_data to get different properties other than lat lon too.

您可以修改return语句以索引到geo_data以获得除lat lon之外的其他属性。

Hope this helps someone.

希望这有助于某人。

R

[R

#4


4  

I didn't find a way to use the existing geocode function (from the ggmap library) to answer this question, so I just created a new function to just do this myself using the existing getURL function (from the RCurl library) and the fromJSON function (from the RJSONIO library).

我没有找到一种方法来使用现有的地理编码功能(来自ggmap库)来回答这个问题,所以我刚刚创建了一个新函数来自己使用现有的getURL函数(来自RCurl库)和fromJSON功能(来自RJSONIO库)。

Write the new function:

写新功能:

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)
}

Test: getGeoData("San Francisco")

测试:getGeoData(“旧金山”)

This gives you a list with the same data that's almost (but not quite) in the same exact format as the list produced by geocode("San Francisco").

这为您提供了一个列表,其中的数据几乎(但不完全)与geocode(“旧金山”)生成的列表的格式完全相同。

#1


12  

With ggmap version 2.7 or greater (as of 13 Dec, not yet available on Cran, but you can install with devtools::install_github("dkahle/ggmap"), you simply need to run register_google(key = 'LONG KEY STRING') and then you can call any of the ggmap functions such as geocode or mutate_geocode and use your API key.

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

#2


12  

I've written the package googleway to access google maps API where you can specify your api key.

我已经编写了googleway软件包来访问谷歌地图API,您可以在其中指定您的API密钥。

For example

例如

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"

#3


5  

Thanks for this! It helped me along immensely. Your solution is pretty specific, so I wanted to include the adaptations I made of your function. It threw bugs because raw_data and geo_data_list are undefined. I'm guessing these were specific to your local environment.

谢谢你!它极大地帮助了我。您的解决方案非常具体,因此我希望包含我对您的功能进行的调整。它抛出了错误,因为raw_data和geo_data_list未定义。我猜这些是特定于您当地的环境。

For me, inputting a location and returning the lat, lon worked with this:

对我来说,输入一个位置并返回lat,lon使用了这个:

 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)
}

You can modify the return statement to index into geo_data to get different properties other than lat lon too.

您可以修改return语句以索引到geo_data以获得除lat lon之外的其他属性。

Hope this helps someone.

希望这有助于某人。

R

[R

#4


4  

I didn't find a way to use the existing geocode function (from the ggmap library) to answer this question, so I just created a new function to just do this myself using the existing getURL function (from the RCurl library) and the fromJSON function (from the RJSONIO library).

我没有找到一种方法来使用现有的地理编码功能(来自ggmap库)来回答这个问题,所以我刚刚创建了一个新函数来自己使用现有的getURL函数(来自RCurl库)和fromJSON功能(来自RJSONIO库)。

Write the new function:

写新功能:

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)
}

Test: getGeoData("San Francisco")

测试:getGeoData(“旧金山”)

This gives you a list with the same data that's almost (but not quite) in the same exact format as the list produced by geocode("San Francisco").

这为您提供了一个列表,其中的数据几乎(但不完全)与geocode(“旧金山”)生成的列表的格式完全相同。