Google 用 R 放置
Google Place with R
我的目的是用R查询googleapi。
我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储:
"latitude"、"longitude"、"business type"
我的想法是使用google地方api:
-使用文本搜索请求搜索地址以存储纬度和经度
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=address&key=AddYourOwnKeyHere
-然后使用 Radar Search Requests 搜索 name near the latitude e longitude ].所以我得到 ID_place
https://maps.googleapis.com/maps/api/place/radarsearch/json?location=latitude,longitude&radius=1&keyword=name&key=AddYourOwnKeyHere
-感谢 ID_place,我查询地点详细信息请求以获取有关业务类型的详细信息(例如 "types" : [ "food" ] )
https://maps.googleapis.com/maps/api/place/details/json?placeid=ID_place&key=AddYourOwnKeyHere
我刚开始使用 google api,这可能不是最有效的方法。
但我需要考虑一下:
-有时地址不完整(有时我缺少公民号码)
- 名称并不总是正确的(有时我有缩写)
我需要了解一些事情:
- -如果这是最好的方法
- -如何用R
查询这些Googleapi
- -如何处理 json 输出
您可以将 ggmap 包与类似的东西一起使用:
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)
}
您可以使用库 "httr" 从 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")
然后您可以使用库 "jsonlite" 来处理结果:
myDataframe<- jsonlite::fromJSON(content(res,"text"))
要访问 R 中的 Google 位置 API,您可以使用我的 googleway 包,特别是 google_places()
.
这也会为您处理 JSON 输出。
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"
如果您想了解有关给定地点的更多详细信息,可以使用 google_place_details()
搜索从上一个查询返回的 place_id
之一
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
我的目的是用R查询googleapi。 我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储:
"latitude"、"longitude"、"business type"
我的想法是使用google地方api:
-使用文本搜索请求搜索地址以存储纬度和经度
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=address&key=AddYourOwnKeyHere
-然后使用 Radar Search Requests 搜索 name near the latitude e longitude ].所以我得到 ID_place
https://maps.googleapis.com/maps/api/place/radarsearch/json?location=latitude,longitude&radius=1&keyword=name&key=AddYourOwnKeyHere
-感谢 ID_place,我查询地点详细信息请求以获取有关业务类型的详细信息(例如 "types" : [ "food" ] )
https://maps.googleapis.com/maps/api/place/details/json?placeid=ID_place&key=AddYourOwnKeyHere
我刚开始使用 google api,这可能不是最有效的方法。 但我需要考虑一下: -有时地址不完整(有时我缺少公民号码) - 名称并不总是正确的(有时我有缩写)
我需要了解一些事情:
- -如果这是最好的方法
- -如何用R 查询这些Googleapi
- -如何处理 json 输出
您可以将 ggmap 包与类似的东西一起使用:
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)
}
您可以使用库 "httr" 从 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")
然后您可以使用库 "jsonlite" 来处理结果:
myDataframe<- jsonlite::fromJSON(content(res,"text"))
要访问 R 中的 Google 位置 API,您可以使用我的 googleway 包,特别是 google_places()
.
这也会为您处理 JSON 输出。
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"
如果您想了解有关给定地点的更多详细信息,可以使用 google_place_details()
搜索从上一个查询返回的 place_id
之一
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