我正在开展一个公共卫生项目,我正在为整个烟草商店刮刮整个州。我一直在尝试使用google places API网络服务,特别是Nearby and Text methods。下面是一些快速的R代码(需要一些错误检查,但运行),以向您展示我正在做的两个版本。
我发现关键字搜索的行为似乎与名称搜索相似 - 我的结果是" vape"在附近的搜索请求或文本搜索请求过于严格,只给我,例如" Vape Escape"," Cloud Vape Lounge"和" Juice Vapeorium" - 但是当我手动搜索谷歌地图中的相同位置并搜索"vape"或"vape near me"时,我会得到十几个更好的结果,包括" Vapor girl"和" Hazmat"没有" vape"在名称中。
任何想法我做错了,API似乎是将我的关键字请求解释为名称请求?
library(RCurl)
library(RJSONIO)
# NOTE: Radar search may be better here, eventually. https://developers.google.com/places/web-service/search
place_url = function(lat, lon, rad = 5000, kw, return.call = "json", sensor = "false") {
root = "https://maps.googleapis.com/maps/api/place/nearbysearch/"
api_key = "MY KEY WENT HERE"
u <- paste(root, return.call, "?location=", lat, ",", lon, "&radius=", rad, "&keyword=", kw, "&key=", api_key, sep = "")
return(URLencode(u))
}
text_url = function(lat, lon, rad = 5000, kw, return.call = "json", sensor = "false") {
#10X multiplier! Be careful!
root = "https://maps.googleapis.com/maps/api/place/textsearch/"
api_key = "MY KEY WENT HERE"
u <- paste(root, return.call, "?location=", lat, ",", lon, "&radius=", rad, "&query=", kw, "&key=", api_key, sep = "")
return(URLencode(u))
}
place_scrape <- function(keyword, lat, lon, radius=35405.6, verbose=TRUE) {
# radius=35405.6: ~=22 miles. circular radius means ~11x11 mile square w/ overlap. Treat like 10x10, dump dups.
df = data.frame(name=character(), lat=double(), lon=double(), id=character(), vicinity=character()) #for starters
if(verbose) cat("Looking for '", keyword,"' around lat", lat, "and lon", lon, "\n")
#lat = 35.917217; lon = -79.054554; radius = 35405.6; keyword = "vape near me"; verbose=T
u=place_url(lat,lon, radius, keyword)
#u=text_url(lat,lon, radius, keyword)
doc <- getURL(u)
x <- fromJSON(doc,simplify = FALSE)
length(x)
if(verbose) cat("Found", length(x), "results.\n")
for (i in 1:length(x)){
result = data.frame(name=x$results[[i]]$name,
lat= x$results[[i]]$geometry$location$lat, lon = x$results[[i]]$geometry$location$lng,
id = x$results[[i]]$id,
# vicinity=x$results[[i]]$formatted_address) #text version
vicinity=x$results[[i]]$vicinity) #place version
df = rbind(df, result)
}
if(verbose) print(df)
return(df)
}
# 35.917217, -79.054554 is chapel hill
results = place_scrape("vape", 35.917217, -79.054554)
results = place_scrape("vapor", 35.917217, -79.054554)
results = place_scrape("vapes", 35.917217, -79.054554)
#scrape maryland: from 40N to 38.5. ~360 miles across by