Skip to content
Prev 308863 / 398503 Next

Extracting results from Google Search

Hi Eduardo

 Scraping the coordinates from the HTML page can be a little tricky
in this case.  Also, Google may not want you using their search engine
for that. Instead, you might use their Geocoding API
(https://developers.google.com/maps/documentation/geocoding),
but do ensure that this fits within their terms of use.

If you do use the Geocoding API, you can do with the following code:

library(RJSONIO)
library(RCurl)

DB<-data.frame(town=c('Ingall', 'Dogondoutchi', 'Tera'),
               country=rep('Niger',3))

location = with(DB, paste(town, country))

ans = lapply(location,
              function(loc)
                 fromJSON(getForm("http://maps.googleapis.com/maps/api/geocode/json",
                              address = loc, sensor = "false"))$results[[1]]$geometry$location
             )

DB = cbind(DB, do.call(rbind, ans))

And now the data frame has the lat and lng variables.

Again, check that the Geocoding terms of use allows you to do this.

 HTH
   D.
On 10/23/12 6:33 AM, ECAMF wrote: