2015-12-21 123 views
12

我試圖使用函數庫中的geocode函數來獲取特定位置的座標。到目前爲止,我可以使用這個功能。增加ggmap的地理編碼功能(R中)的api限制

我遇到的問題是我想將我的每日限額從2,500增加到100,000。官方Google documentation 表示,如果您對該項目啓用記帳功能,我很樂意這樣做。當您繼續此過程時,Google Developers Console會爲您提供個性化的API密鑰。

但是,geocode函數沒有選項來放入此個性化的API密鑰。相反,它要求client(商業用戶的客戶端ID)和signature(商業用戶的簽名),這就是Google Maps API for Work客戶如何訪問API。我得到的,這也是一種選擇,但是,這似乎是一個非常實用的情況下,由於工作谷歌地圖API似乎是專爲大型企業帳戶:

每日配額開始在每24小時100,000個請求,基於年度合同購買。

所以我的問題歸結爲:我可以使用geocode功能從ggmapsR ping到谷歌地圖API地理編碼?

回答

4

我沒有找到一種方法,使用現有的geocode功能(從ggmap庫)來回答這個問題,所以我剛剛創建了一個新的功能,只是這樣做使用現有getURL功能(從RCurl庫自己)和fromJSON函數(來自RJSONIO庫)。

寫入新功能:

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("San Francisco")

這給了你,在爲所產生的名單完全相同的格式幾乎(但不完全)相同的數據列表geocode("San Francisco")

5

感謝您的支持!它極大地幫助了我。 你的解決方案非常具體,所以我想包括我對你的功能進行的改編。它拋出了錯誤,因爲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) 
} 

您可以修改return語句索引geo_data得到比緯度經度不同的屬性了。

希望這可以幫助別人。

[R

8

我已經寫了包googleway訪問谷歌地圖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" 
7

2.7或更高版本(ggmap版本12月13日,中尚未公佈的克蘭,但你可以devtools::install_github("dkahle/ggmap")安裝,你只需要運行register_google(key = 'LONG KEY STRING'),然後你可以調用任何的ggmap函數,如geocodemutate_geocode並使用您的API密鑰。