Matias Andina
Matias Andina

Reputation: 4220

Retrieve Location coordinates from google maps in R

I want to retrieve the locations of a search term from google maps.

Here's an example with the term "pizza" near London

I would like to get the results of latitude and longitude for every place. I've been searching and this is possible if you have the address.

Here's an example using address.

However I am interested in using a search term just like you would do when you search for a term in Google Maps.

Upvotes: 4

Views: 3767

Answers (1)

Matias Andina
Matias Andina

Reputation: 4220

I went to Google Places API as suggested in comments. I managed to create an account and get a key. I wrote a function named encontrar that basically searches for places with a keyword in a radius from a center. I order to get the center I went to google maps and found latitude and longitude for that point. This was very quick to do by hand I don't think I will need to write code for that but be my guest. So once the center is defined,

coordenadas<-c(-34.605424, -58.458499)

encontrar<-function(lugar,radius,keyword){

# radius in meters
#lugar is coordinates from google maps by hand
  coor<-paste(lugar[1],lugar[2],sep=",")
  baseurl<-"https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
  google_key<-c(" YOUR KEY HERE")


q<-paste(baseurl,"location=",coor,"&radius=",radius,"&types=food|restaurant&keyword=",keyword,"&key=",google_key, sep="")

print(q)

data1<-fromJSON(q)

lat_long<-data.frame(lat=data1$results$geometry$location$lat,long=data1$results$geometry$location$lng)

#print(data1)

sitios<-data1$results$name

df<-cbind(sitios,lat_long)
return(df)
}


encontrar(lugar = coordenadas,radius = 500,"pizzeria")

gives a nice data frame with the places within 500m

It's also worth to mention that "&types=food|restaurant" help in this case because the keyword is "pizzeria". It should be changed in other case.

Upvotes: 5

Related Questions