我想通过他们的名字来获取商家的坐标。我已经回顾了几个关于使用地理编码'的问题。但他们似乎都基于地址工作。请参阅以下两个试图获取伦敦韦斯特伯里酒店坐标的例子:
library(ggmap)
geocode("London")
geocode("The Westbury Hotel London") # Returns coordinates of Westbury Road in London
更复杂的方法:
require(RJSONIO)
library(ggmap)
geocodeAddress <- function(address) {
require(RJSONIO)
url <- "http://maps.google.com/maps/api/geocode/json?address="
url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
x <- fromJSON(url, simplify = FALSE)
if (x$status == "OK") {
out <- c(x$results[[1]]$geometry$location$lng,
x$results[[1]]$geometry$location$lat)
} else {
out <- NA
}
Sys.sleep(0.2) # API only allows 5 requests per second
out
}
geocodeAddress("The Westbury Hotel London") # Returns London coordinates
其他questions提到可以从“地理编码”的地方获取坐标。但是,至少在我的情况下,它不起作用。关于如何通过谷歌地图获取商家名称坐标的任何想法都非常受欢迎。
答案 0 :(得分:1)
您可以使用Google Places API使用我的googleway
包搜索地点。您必须对结果进行一些处理,或者如果您希望获得您之后的确切业务,则需要优化您的查询,因为API通常会返回多个可能的结果。
您需要Google API密钥才能使用其服务
library(googleway)
## your API key
api_key <- "your_api_key_goes_here"
## general search on the name
general_result <- google_places(search_string = "The Westbury Hotel London",
key = api_key)
general_result$results$name
# [1] "The Westbury" "Polo Bar" "The Westbury"
general_result$results$geometry$location
# lat lng
# 1 53.34153 -6.2614740
# 2 51.51151 -0.1426609
# 3 51.59351 -0.0983930
## more refined search using a location
location_result <- google_places(search_string = "The Wesbury Hotel London",
location = c(51.5,0),
key = api_key)
location_result$results$name
# [11] "The Marylebone" "The Chelsea Harbour Hotel"
# "Polo Bar" "The Westbury" "The Gallery at The Westbury"
location_result$results$geometry$location
# lat lng
# 1 51.51801 -0.1498050
# 2 51.47600 -0.1819235
# 3 51.51151 -0.1426609
# 4 51.59351 -0.0983930
# 5 51.51131 -0.1426318
location_result$results$formatted_address
# [1] "37 Conduit St, London W1S 2YF, United Kingdom" "37 Conduit St, London, Mayfair W1S 2YF, United Kingdom"
# [3] "57 Westbury Ave, London N22 6SA, United Kingdom"