我的目的是使用R来查询google api。 我有一个地址和名称列表(属于商店、餐馆等),我需要存储每个地址和名称:
“纬度”、“经度”、“业务类型”
我的想法是使用 google place api:
-使用文本搜索请求搜索地址以存储纬度和经度
https://maps.googleapis.com/maps/api/place/textsearch/xml?query=address&key=AddYourOwnKeyHere
-然后使用雷达搜索请求搜索纬度和经度附近的名称。所以我得到了 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 中的 Google Places 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
您可以使用库“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"))
您可以将 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)
}
您可以使用
newplaces
,这是 Google Places API 的 R 包装程序包(新 API 于 2023 年底发布)。
在您的示例中,您可以运行以下命令:
# Install package from github:
remotes::install_github("aTnT/newplaces")
library(newplaces)
# Set your API key:
set_api_key(key = "YOUR_MAPS_API_KEY")
# Get restaurants within a 500 m radius around a location:
myPlaces <- text_search(textQuery = "restaurants", location = "bias", circle_center_latitude = 40.75797, circle_center_longitude = -73.98554, circle_radius = 500)
您可以通过
myPlaces$places
: 的位置访问 tibble 数据框
myPlaces$places
# A tbl_json: 20 x 77 tibble with a "JSON" attribute
..JSON document.id name id nationalPhoneNumber internationalPhoneNu…¹ formattedAddress rating googleMapsUri
<chr> <int> <chr> <chr> <chr> <chr> <chr> <dbl> <chr>
1 "{\"n… 1 plac… ChIJ… (212) 221-3800 +1 212-221-3800 200 W 44th St, … 4.5 https://maps…
2 "{\"n… 2 plac… ChIJ… (212) 302-2000 +1 212-302-2000 1515 Broadway @… 4.4 https://maps…
3 "{\"n… 3 plac… ChIJ… <NA> <NA> 229 W 43rd St, … 4.8 https://maps…
4 "{\"n… 4 plac… ChIJ… (212) 343-3355 +1 212-343-3355 1501 Broadway, … 4.4 https://maps…
5 "{\"n… 5 plac… ChIJ… (212) 918-1330 +1 212-918-1330 1567 Broadway, … 4 https://maps…
6 "{\"n… 6 plac… ChIJ… (332) 249-8600 +1 332-249-8600 1440 Broadway, … 4.3 https://maps…
7 "{\"n… 7 plac… ChIJ… (212) 333-3254 +1 212-333-3254 2 Times Sq, New… 4.1 https://maps…
8 "{\"n… 8 plac… ChIJ… (212) 581-6464 +1 212-581-6464 326 W 46th St, … 4.4 https://maps…
9 "{\"n… 9 plac… ChIJ… (212) 921-2400 +1 212-921-2400 620 8th Ave, Ne… 4.2 https://maps…
10 "{\"n… 10 plac… ChIJ… (212) 354-5013 +1 212-354-5013 136 W 46th St, … 4.6 https://maps…
11 "{\"n… 11 plac… ChIJ… (646) 434-2448 +1 646-434-2448 575 7th Ave, Ne… 4.5 https://maps…
12 "{\"n… 12 plac… ChIJ… <NA> <NA> 135 W 50th St, … 3.9 https://maps…
13 "{\"n… 13 plac… ChIJ… (917) 565-9044 +1 917-565-9044 132 W 43rd St, … 4.4 https://maps…
14 "{\"n… 14 plac… ChIJ… (646) 435-0135 +1 646-435-0135 691 8th Ave, Ne… 4.4 https://maps…
15 "{\"n… 15 plac… ChIJ… (212) 869-3965 +1 212-869-3965 67 W 44th St, N… 4.2 https://maps…
16 "{\"n… 16 plac… ChIJ… (212) 597-5126 +1 212-597-5126 121 W 45th St, … 4.4 https://maps…
17 "{\"n… 17 plac… ChIJ… (212) 997-1270 +1 212-997-1270 36 W 48th St, N… 4.2 https://maps…
18 "{\"n… 18 plac… ChIJ… (212) 265-5400 +1 212-265-5400 155 W 43rd St, … 4.2 https://maps…
19 "{\"n… 19 plac… ChIJ… (212) 398-7440 +1 212-398-7440 151 W 46th St, … 4.3 https://maps…
20 "{\"n… 20 plac… ChIJ… (212) 997-4540 +1 212-997-4540 254 W 47th St, … 4.4 https://maps…
# ℹ abbreviated name: ¹internationalPhoneNumber
# ℹ 68 more variables: websiteUri <chr>, utcOffsetMinutes <dbl>, adrFormatAddress <chr>, businessStatus <chr>,
# priceLevel <chr>, userRatingCount <dbl>, iconMaskBaseUri <chr>, iconBackgroundColor <chr>, takeout <lgl>,
# delivery <lgl>, dineIn <lgl>, curbsidePickup <lgl>, reservable <lgl>, servesBreakfast <lgl>,
# servesLunch <lgl>, servesDinner <lgl>, servesBeer <lgl>, servesWine <lgl>, servesBrunch <lgl>,
# servesVegetarianFood <lgl>, primaryType <chr>, shortFormattedAddress <chr>, outdoorSeating <lgl>,
# liveMusic <lgl>, menuForChildren <lgl>, servesCocktails <lgl>, servesDessert <lgl>, servesCoffee <lgl>, …
如果您的
includedType
指定了其他内容,您还可以使用函数参数 textQuery
来指定业务类型。