Plot Google Maps images in R using geocoding {dismo}

Google Maps image downloaded with R

Hello there!
It’s been a while, but after a short creative break I am back with an exciting new tutorial: Plotting google maps images in R using geocoding with the help of the {dismo} package.

{dismo} is actually a package for Species Distribution Modeling but it also includes a function that is a wrapper around the Google geocoding web-service. And this is how it works:

Install packages

First you have to install and load the {dismo} library into R. Another package that will be useful for following visualisation steps is the {scales} package. Use the following commands to install and load them into R:


Geocoding in R

Once this is done, think of any place you would like to visit right now, type the adress down and assign it to the variable myPlace. In my case it’s going to be the famous square “Stephansplatz Wien” in Austria. After that it’s time to use the function geocode().

myPlace <- "Stephansplatz Wien"
x <- geocode(myPlace)

The function returns a data.frame containing the following:

  • originalPlace: the locality description as provided (in argument x)
  • interpretedPlace: the locality as interpreted by the Google API
  • lon: longitude
  • lat:  latitude
  • lonmin: minimum longitude of the bounding box
  • lonmax: maximum longitude of the bounding box
  • latmin: minimum latitude of the bounding box
  • latmax: maximum latitude of the bounding box
  • uncertainty: distance from c(lon, lat) to the farthest corner of the bounding box


It is important to compare the fields originalPlace and interpretedPlace as the Google interpretation of a (perhaps vague) locality description can be very speculative.

Especially the fields lonmin, lonmax, latmin and latmax are very useful for our next step:

Download google maps imagery

We are going to use these four coordinates as a bounding box for the google imagery download. First let’s make the bounding box a little bigger by adding and substracting some values. x[5:8] extracts the four mentioned values and then the extent() function from the {raster} package is used to create a extent object of a Raster:

a <- x[5:8] + c(-0.002, 0.002, -0.002, 0.002)
e <- extent(as.numeric(a))

Once this is done, we can finally download the google image with the gmap() function. Simply plug in the extent object into the function and specify the type of google maps imagery you want to download by setting the type argument to either: ‘roadmap’, ‘satellite’, ‘hybrid’, ‘terrain’.

g <- gmap(e, type = "satellite")

When you call the plot() function on the result, this will yield in the following plot:

Google maps image of Vienna

Google maps image of Vienna

Taking it one step further

You can now download the image using the writeRaster() function from the {raster} package or you can further modify the image by overlaying points, polygons or text. Here is an example of a google maps image showing Brisbane, AUS with some text overlay:

x <- geocode('Kangoroo Point Brisbane')
a <- x[5:8] + c(-0.006, 0.006, -0.002, 0.002)
e <- extent(as.numeric(a))
g <- gmap(e, type = "satellite", rgb=T)

     labels="Brisbane", col=alpha("white",0.5), cex=4)


Google maps image of Brisbane, AUS

Google maps image of Brisbane, AUS

I hope you enjoyed this tutorial. Please let me know if you have any troubles or any questions regarding the {dismo} or {raster} package.



About This Author

Martin was born in Czech Republic and studied at the University of Natural Resources and Life Sciences, Vienna. He is currently working at an Earth Observation Company in Austria, specialised in Land Monitoring. His main interests are: Open-source applications like R, (geospatial) statistics and data-management, web-mapping and visualization. He loves travelling, geocaching, photography and TV series.

Post A Reply