问题
My purpose is to use R to query google api. I have a list of addresses and names (belong to shops, restaurants, etc.) and for each of them I need to store:
"latitude", "longitude", "business type"
My idea is to use google place api:
-Use the Text Search Requests to search the address to store latitude e longitude
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=address&key=AddYourOwnKeyHere
-Then use the Radar Search Requests to search the name nearby the latitude e longitude. So I get the ID_place
https://maps.googleapis.com/maps/api/place/radarsearch/json?location=latitude,longitude&radius=1&keyword=name&key=AddYourOwnKeyHere
-Thanks to the ID_place, I query the Place Details Requests to get detail about business type (es. "types" : [ "food" ] )
https://maps.googleapis.com/maps/api/place/details/json?placeid=ID_place&key=AddYourOwnKeyHere
I'm very new to use google api and maybe is not the most efficient way to do it. But I need to give some consideration: -Sometimes the address is not complete (sometimes I have the civic number missing) -Not always the name is correct (sometimes I have abbreviations)
I need to understand some things:
- -If this is the best way to do it
- -How query these Google api with R
- -How handle the json output
回答1:
To access the Google Places API in R you can use my googleway package, and in particular google_places()
.
This also handles the JSON output for you.
library(googleway)
key <- 'your_api_key_goes_here'
df_places <- google_places(search_string = "cafe",
location = c(-37.81827, 144.9671), ## melbourne, AU
key = key)
df_places$results$name
# [1] "Time Out Fed Square" "Dukes Coffee Roasters" "Alice Nivens" "Little Cupcakes" "Lindt Chocolate Cafe" "Cafe Andiamo"
# [7] "The Journal Cafe" "RMB Cafe Bar" "Cafe Issus" "ACMI Cafe & Bar" "Ponyfish Island" "Aix Cafe"
# [13] "Seedling Cafe" "Eliana Lulu" "B3 Cafe" "Lindt Chocolate Cafe" "Switch Board Cafe" "Urban Express Cafe"
# [19] "Cento Mani" "1932 Cafe & Restaurant"
If you want more details about a given place you can use google_place_details()
to search for one of the place_id
s returned from the previous query
df_details <- google_place_details(place_id = df_places$results[1, "place_id"],
key = key)
df_details$result
# [1] "restaurant" "cafe" "bar" "food" "point_of_interest" "establishment"
df_details$result$reviews
# aspects author_name author_url language rating
# 1 0, overall Fredrich Oliver-bently https://plus.google.com/114792371400821038660 en 2
# 2 0, overall Jenn Besonia https://plus.google.com/110502657363574676299 en 2
# 3 0, overall Sewa G S R https://plus.google.com/118332347441422887680 en 1
# 4 0, overall M Mathumbu https://plus.google.com/104636428392041496439 en 2
# 5 2, overall Bo Cui https://plus.google.com/104475569220729624507 en 4
回答2:
You could use library "httr" to get the result from google API:
res<-GET( "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=35.325153,-80.946239&radius=5000&name=YOURNAME&key=YOURKEY")
jsonAnsw<-content(res,"text")
then you could use library "jsonlite" to handle the result:
myDataframe<- jsonlite::fromJSON(content(res,"text"))
回答3:
You can use ggmap package with something like :
library('ggmap')
dataToMap = rbind(lon=longitude, lat=latitude)
geo.df = NULL
for(i in 1:length(dataToMap[,1]){
location = geocode(dataToMap[1,],override_limit=TRUE,messaging=FALSE)
geo.df = rbind(lon=location$lon[1], lat=location$lat[1])
Sys.sleep(1.5)
}
来源:https://stackoverflow.com/questions/28026897/google-place-with-r