Geocoding is the process of assigning geographic coordinates (e.g. latitude-longitude) to a street address, city, a country as well as other points and features. With geographic coordinates, the features can then be mapped and entered into Geographic Information Systems. Geocoding is now popular on web and mobile based applications because the use of GPS enabled smartphones have increased greatly over the years.

Google Geocoding Web Service

There are many map services found on the Internet. Among them, Google is the most popular and broadly used because of its map coverage around the world. Google offers the Map Service to general users and also a rich API to integrate the map service to different applications. The Google Maps JavaScript API v3 introduced a new format for geocoding responses that offer a number of improvements over the format used in the v2 API. To name a few:

  • A flatter response format for address components that is easier to parse

  • The ability to tag an address component with multiple types

  • Both full names and abbreviations for countries and states

  • Differentiation between rooftop and interpolated geocoder results

  • Both the bounding box and recommended viewport for each result

Geocoding with Google Maps API

A simple map view showing markers

The Geocoding Web Service is intended to enable pre-caching of Geocoder results that one knows their application will need in the future. For example, if someone’s application displays property listings, he can geocode the address of each property, cache or save the results on his server, and serve these locations to his API application. This ensures that his application does not need to geocode the address of a property every time it is viewed by a user. It will help to improve the performance drastically.

How to use Geocoding:

Google Map API provides geocoding data through XML and JSON format. The searching in the maps API is made easy through string format. As a result, any valid location (e.g Street Address, City, State/Province, Zip Code, Country Name) returns something. So if someone is not sure about the street address, Google Maps API is intelligent enough to bring results based on the city and country.  For example, if you search Dhaka city, it will return all the relevant formatted data to show the locations on a map.

http://maps.google.com/maps/api/geocode/json?address=dhaka&sensor=false

Latitude and longitude information is necessary to manipulate the drawing of the map in the application. Similarly, reverse Geocoding is also possible in Maps API through latitude and longitude.

http://maps.google.com/maps/api/geocode/json?latlng=23.709921,90.407142&sensor=false

Storing the results in cache or database is recommended to draw the map canvas using the Maps API and reduce the number of server requests.

Drawing Map Canvas

With advanced geocoding technology, applications can improve marketing campaigns by mapping where consumers go beyond just their billing address – such as work, commuting, shopping, recreation, and vacation – and determine prime target areas for promotional activities. Geocoding also allows the customers to visualize demographics. Google Map API’s rich drawing options make these things possible.  Google Map API V3 provides a list of various drawing functions to go with the user’s demand. You can find the complete guide from Google Developers. It has rich map functions for Web, iOS, and Android platforms. For example, Google can automatically find routes between point A and B. Let’s find out how it will be viewed on the map.

Javascript Code:

Results showed on map canvas:
Google Map

Google Map

Geocoding in Rails

Geocoding and reverse Geocoding is made very simple in rails by the Geocoder Gem. By default, the gem uses Google Maps API, but other API services like Yahoo, Bing etc can also be configured. Model, holding address fields can be geocoded directly to get latitude and longitude. This gem also supports query of records, which are nearby to the desired location.

We hope that you find the new Geocoding Web Service easier to use and useful. As always we encourage you to check out the Google Maps API Group if you have any questions or comments related to the APIs. We look forward to seeing more great features to the Geocoding Web Service in future and your exciting implementations based on that!

Contributor: Saad Naufel Rahman, Software Engineer, Nascenia
Published On: December 26th, 2013 / Categories: Blog, Ruby on Rails, Technology / Tags: , , /