Google Place with R [英] Google Place with R
问题描述
我的目的是用R来查询google api。
我有一个地址和名字的列表(属于商店,餐厅等),并且我需要存储每个人的名字:
纬度 ,经度,商业类型
我的想法是使用google的地方api:
- 使用文本搜索请求搜索地址以存储纬度经度
https://地图。 googleapis.com/maps/api/place/textsearch/xml?query=address&key=AddYourOwnKeyHere
- 然后使用雷达搜索请求搜索纬度 e 经度附近的名称。所以我得到ID_place
https://maps.googleapis.com/maps/api/place/radarsearch/json?location =经度&半径= 1&关键字=名称&键= AddYourOwnKeyHere
- 感谢 ID_place ,我查询地点详细信息请求以获取有关业务类型的详细信息(例如types:[food])
https://maps.googleapis.com/maps/api/place/details/json?placeid=ID_place&key=AddYourOwnKeyHere
我很新使用google api,也许不是最有效的方法。
但我需要考虑一下:
- 有时候地址不完整(有时我缺少公民号)
- 不一定名称是正确的(有时候我有缩写)
我需要了解一些内容:
>
要访问R中的Google Places API,您可以使用我的 googleway 包,特别是 google_places()
。
$ b
library(googleway)
key< - ' your_api_key_goes_here'
df_places< - google_places(search_string =cafe,
location = c(-37.81827,144.9671),## melbourne,AU
键=键)
df_places $结果$名称
#[1]Time Out Fed SquareDukes Coffee RoastersAlice NivensLittle CupcakesLindt巧克力咖啡厅咖啡厅Andiamo
#[7]The Journal CafeRMB咖啡厅酒吧Cafe IssusACMI Cafe&酒吧Ponyfish IslandAix Cafe
#[13]幼苗咖啡馆Eliana露露B3咖啡厅Lindt巧克力咖啡馆开关板咖啡馆都市快车咖啡馆
# [19]Cento Mani1932 Cafe&餐厅
如果您想了解更多关于某个地点的详细信息,您可以使用 google_place_details ()
来搜索从前一个查询返回的 place_id
s中的一个
df_details< - google_place_details(place_id = df_places $ results [1,place_id],
key = key)
df_details $ result
#[1]餐厅咖啡厅酒吧食物兴趣点建立
df_details $结果$评论
#方面author_name author_url语言评分
#1 0,整体Fredrich Oliver-bently https://plus.google.com/114792371400821038660 en 2
#2 0,整体Jenn Besonia https://plus.google.com/110502657363574676299 zh 2
#3 0,整体Sewa GSR https://plus.google.com/118332347441422887680 zh 1
# 4 0,总数M Mathumbu https://plus.google.com/104636428392041496439 zh 2
#5 2,整体博翠https://plus.google.com/104475569220729624507 zh 4
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
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
这篇关于Google Place with R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!