Led & Sustained by

G2one Logo

Developed with

Intellij

Powered by

Spring

PostCode Plugin

PostCode Plugin

This plugin provides a single artefact, the PostCodeService. This lets you look up a UK postcode string and retrieve the lat/lon coordinates for the "centre of the postcode sector". The data is free, from http://www.nearby.org.uk, and has over 10,000 postcode centre points defined. It may however not be complete, and currently operates at the postcode sector level, not street level.

You can also retrieve the distance between two postcodes, or two lat/lon points, so that you can perform proximity searches etc on UK data without having to use a geocoding service such as Google Maps API.

Usage:

class LocalSearchController {
   def postCodeService

   def findByPostcodeWithin10km = {
       def userLocation = postCodeService.getPostCodeCentre(params.postCode)
       assert userLocation, "Ooops postcode not found in DB"

       model.result = []
       BookShop.list().each() { shop ->
           def location = postCodeService.getPostCodeCentre(shop.postCode)
           def distInKM = postCodeService.getDistanceBetweenPoints(userLocation.centre, location.centre)
           // Is it within 10km?
           if (distInKM < 10)
              model.results << shop
       }   
       render(view:"results", model:model)
   }

   def getDistanceBetweenPostCodes = {
       def d = postCodeService.getDistanceBetweenPostCodes(params.postCodeA, params.postCodeB)
       render(view:"distanceResult", model:[distance: d])
   }
}
</