Advertisement
Guest User

Untitled

a guest
Aug 12th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.18 KB | None | 0 0
  1. package com.geocode
  2.  
  3.  
  4. import groovyx.net.http.HTTPBuilder
  5. import org.apache.http.client.ClientProtocolException
  6. import org.apache.log4j.Logger
  7. import org.codehaus.groovy.grails.web.util.WebUtils
  8.  
  9. class GeocodingUtil {
  10.  
  11.     private static final Logger log = Logger.getLogger(GeocodingUtil.class);
  12.     public static final String GEOCODING_API_HOST = 'https://maps.googleapis.com'
  13.  
  14.     private static getLookupService() {
  15.         return grails.util.Holders.applicationContext.getBean('geoLookupService')
  16.     }
  17.  
  18.     private static _googleApiKey
  19.     private static getGoolgeApiKey(){
  20.         if(_googleApiKey == null){
  21.             _googleApiKey = grails.util.Holders.config.google.apiKey.isEmpty() ?
  22.                     '' : grails.util.Holders.config.google.apiKey
  23.         }
  24.         return _googleApiKey
  25.     }
  26.  
  27.     public static Object getAddressBoundingBox(String address) {
  28.         return getResult(address).results.geometry.bounds[0]
  29.     }
  30.  
  31.     public static String getIpBasedLocation() {
  32.         def request = WebUtils.retrieveGrailsWebRequest().request
  33.         def isLocal = request.remoteAddr.toString().equals("127.0.0.1") || request.remoteAddr.toString().startsWith("0:0")
  34.  
  35.         def location = isLocal ? getLookupService().getLocation("178.222.107.131") : getLookupService().getLocation(request.remoteAddr)
  36.         def sRegion = location != null && location.region != null && location.region != "00" ? "${location.region}" : "",
  37.             sCity = location != null && location.city != null ? "${location.city}," : "",
  38.             sCountry = location != null && location.countryName != null ? location.countryName : "",
  39.             ipLocation = "${sCity} ${sRegion} ${sCountry}"
  40.         return ipLocation
  41.     }
  42.  
  43.  
  44.     public static Object getLatLng(Location loc) {
  45.         def location = null
  46.         loc.stringVariations().each { String variation ->
  47.             if (location != null) {
  48.                 return
  49.             }
  50.  
  51.             log.debug("Geocoding '${variation}'")
  52.             def result = getResult(variation).results.geometry.location
  53.             log.debug("Result:" + result)
  54.  
  55.             if (result.size() > 0) {
  56.                 location = [lat: result.lat[0], lng: result.lng[0]]
  57.             }
  58.         }
  59.         return location;
  60.     }
  61.  
  62.     public static Object getLatLng(String address) {
  63.         log.debug("Geocoding '${address}'")
  64.         def result = getResult(address).results.geometry.location
  65.         return [lat: result.lat[0], lng: result.lng[0]]
  66.     }
  67.  
  68.  
  69.     public static getResult(String query) {
  70.         def rval = null
  71.         try{
  72.             new HTTPBuilder(GEOCODING_API_HOST).get([path: '/maps/api/geocode/json', query: [key : getGoolgeApiKey(), address: query, sensor: false]]) { resp, json ->
  73.                 rval = json
  74.             }
  75.         }catch(ClientProtocolException ex){
  76.             log.error("Protocol error while connecting to  ${GEOCODING_API_HOST}",ex)
  77.         }catch(IOException ex){
  78.             log.error("I/O  error while connecting to  ${GEOCODING_API_HOST}",ex)
  79.         }catch(URISyntaxException ex){
  80.             log.error("URI mallformed: ${GEOCODING_API_HOST}")
  81.         }
  82.  
  83.         log.debug("Geocoding result:" +  rval.toString())
  84.         return rval
  85.     }
  86.  
  87.     public static getStateAndCountry(String query) {
  88.         def rval = [:]
  89.  
  90.         getResult(query).results?.address_components[0].each {
  91.  
  92.             if (it.types.contains('country')) {
  93.                 rval['country'] = it.long_name
  94.             }
  95.             if (it.types.contains('administrative_area_level_1')) {
  96.                 rval['state'] = it.long_name
  97.             }
  98.         }
  99.  
  100.         return rval
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement