3

I am trying to get the coordinates of businesses by their name. I have reviewed several questions on using 'geocode' but they all seem to work based on the address. See below two examples trying to get the coordinates of The Westbury Hotel London:

library(ggmap)
geocode("London")
geocode("The Westbury Hotel London") # Returns coordinates of Westbury Road in London

A more complex approach:

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

Other questions mentioned that it is possible to get coordinates from places with 'geocode' but, at least in my case, it is not working. Any idea on how to get coordinates by business name from google maps hugely appreciated.

Community
  • 1
  • 1
user3507584
  • 3,246
  • 5
  • 42
  • 66

1 Answers1

1

You can use the Google Places API to search for places using my googleway package. You'll have to do some work with the results, or refine your query if you want to get the exact business you're after as the API usually returns multiple possible results.

You need a Google API key to use their service

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"
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139