Grails Cookbook - A collection of tutorials and examples

Grails Google Maps Geocoding Example

This example shows how to translate an address to it's corresponding latitude and longitude values using Google Map's Service.

Manual checking

To geocode a given address using a browser, use the following URL format
http://maps.googleapis.com/maps/api/geocode/json?address=1600 Amphitheatre Pkwy, Mountain View, CA 94043

The response will be in JSON format:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4219951,
               "lng" : -122.0840046
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4233440802915,
                  "lng" : -122.0826556197085
               },
               "southwest" : {
                  "lat" : 37.4206461197085,
                  "lng" : -122.0853535802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

The latitude and longitude information is under the geometry section.

Create Service

To write HTTP client code easier, it is recommended to use the rest plugin. Add the plugin to your BuildConfig:
grails.project.dependency.resolution = {
    ...
    plugins {
        ...
        compile ":rest:0.8"
    }
}
Then we can use withHttp of the rest plugin to create the rest of the geocoding service.
package asia.grails.geosample
class MapService {
    def getLatLong(String address) {
        def result = null
        withHttp(uri: "http://maps.googleapis.com") {
            def html = get(path : '/maps/api/geocode/json', query : [address:address])
            if ( html.results != null && html.results.size() > 0 ) {
                def partial = html.results[0]
                if ( partial.geometry != null && partial.geometry.location != null) {
                    def lat = partial.geometry.location.lat
                    def lng = partial.geometry.location.lng
                    result = [lat:Double.valueOf(lat.toString()), lng:Double.valueOf(lng.toString())]
                }
            }
        }
        return result
    }
}
The service will return the latitude and longitude pair as a map.

Using the service

Using the service is straightforward:
package asia.grails.geosample
class TestController {
    def mapService
    def geocodeTest() {
        def point = mapService.getLatLong('1600 Amphitheatre Pkwy, Mountain View, CA 94043')
        render "latitude = ${point.lat}"
        render "longitude = ${point.lng}"
    }
}