SymbolixAU / googleway

R Package for accessing and plotting Google Maps
http://symbolixau.github.io/googleway/
Other
234 stars 46 forks source link

Is there a way to obtain every locality within a radius? #233

Closed jcrodriguez1989 closed 3 years ago

jcrodriguez1989 commented 3 years ago

I am running:

places <- googleway::google_places(
  location = c(33.90666, -116.549327),
  radius = 30 * 1.60934, # 30 miles to km.
  language = "en",
  key = api_key
)

but my places$results has only 4 rows, of which just one is a locality. However, if I check here there are much more localities within 30miles.

thanks!

dcooley commented 3 years ago

The radius argument is expecting metres, so you're searching a 48.28 metre radius. This probably isn't what you're intending?

jcrodriguez1989 commented 3 years ago

thanks for your reply, that was an issue I had :) I've tried now:

res <- list(next_page_token = NULL)
places <- list()
i <- 0
while ("next_page_token" %in% names(res)) {
  res <- googleway::google_places(
    location = c(33.90666, -116.549327),
    radius = 30 * 1.60934 * 1000, # 30 miles to meters.
    language = "en",
    key = api_key,
    page_token = res$next_page_token
  )
  print(res$status)
  i <- i + 1
  places[[i]] <- res
}

And it still retrieves too few results, only 60 rows (in total). And just 2 of them are localities.

dcooley commented 3 years ago

When you say "every locality", do you mean the locality type as per Google's definition ?

If so you can specify this type in the place_type argument.

jcrodriguez1989 commented 3 years ago

Exactly, I also did it. but just 2 results.

places <- googleway::google_places(
  location = c(33.90666, -116.549327),
  radius = 30 * 1.60934 * 1000, # 30 miles to meters.
  language = "en",
  key = api_key,
  place_type = "locality"
)

It seems something is not working as expected xD

dcooley commented 3 years ago

The url generated by your query is

https://maps.googleapis.com/maps/api/place/nearbysearch/json?&location=33.90666,-116.549327&radius=48280.2&language=en&type=locality&key=

Which as far as I can tell is correct according to google's example

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key=YOUR_API_KEY

so I don't really know why you're getting two results.

jcrodriguez1989 commented 3 years ago

Thank you very much! I am new to the GMaps API, so all I wanted to know was if I was missing something. My next try is to manually define a grid of several (lat, long) points, and start using the google_reverse_geocode for each point in the grid. Do you have any comments about this idea? Any better approach you would try? Thanks for your help! I really appreciate it.

dcooley commented 3 years ago

If you're searching in the US there may be other packges which contain US-specific data, like tidycensus, which will give you boundaries and areas. This might have what you're looking for, and shouldn't cost anything.

jcrodriguez1989 commented 3 years ago

Yup, that would be a great idea, but I need the exact Google locality names 🤦‍♂️ thanks!

jcrodriguez1989 commented 3 years ago

Thank you very much @dcooley , the grid approach did work. Excellent package!