Using Geocoding Google API
Posted on May 20, 2012
Ever wanted to transform an address into longitude and latitude coordinates?
When working with data that can be projected to geographic places, there’s nothing more tempting than trying to visualize it in a map. You just need a set of coordinates (longitude and latitude) and some map visualization tool. So far, so good. The problem arises when the only thing you have is addresses but no longitude and latitude. What can be done in such cases? Meet Geocoding!
According to wikipedia:
geocoding is the process of finding associated geographic coordinates from other geographic data, such as street addresses or zip codes
The idea is very simple. Let’s say we have some address like
1600 Amphitheatre Parkway, Mountain View, CA
, and that we ant to get its geographic
coordinates. By using a geocoding tool we would find that the previous address has
latitude 37.423021 and longitude -122.083739.
What can we use to geocodify our data in R?
An interesting —but not the only— option is to use the Geocoding Google API from Google Maps API Web Services.
If you check the geocoding documentation, you’ll see that we can do requests and obtain the answers in XML format. The required url format that we would need is something like this:
Code in R of a Geocoding API request with XML response
In order to geocode some data in R we need to use the library "XML"
(by
Duncan Temple Lang). Here’s a —simple example — of how to do that:
The obtained results should be: