使用 google 地图按商家名称查找坐标

Find coordinates by business name with google maps

我正在尝试通过名称获取商家的坐标。我已经回顾了几个关于使用 'geocode' 的问题,但它们似乎都基于地址。请参阅以下两个尝试获取伦敦韦斯特伯里酒店坐标的示例:

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

其他 提到可以从具有 'geocode' 的地方获取坐标,但至少在我的情况下,它不起作用。非常感谢任何关于如何从 google 地图中按公司名称获取坐标的想法。

您可以使用 Google Places API to search for places using my googleway 包。您必须对结果做一些处理,或者如果您想获得您所追求的确切业务,则需要优化您的查询,因为 API 通常 returns 多个可能的结果。

您需要 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"