Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.geocode
- import groovyx.net.http.HTTPBuilder
- import org.apache.http.client.ClientProtocolException
- import org.apache.log4j.Logger
- import org.codehaus.groovy.grails.web.util.WebUtils
- class GeocodingUtil {
- private static final Logger log = Logger.getLogger(GeocodingUtil.class);
- public static final String GEOCODING_API_HOST = 'https://maps.googleapis.com'
- private static getLookupService() {
- return grails.util.Holders.applicationContext.getBean('geoLookupService')
- }
- private static _googleApiKey
- private static getGoolgeApiKey(){
- if(_googleApiKey == null){
- _googleApiKey = grails.util.Holders.config.google.apiKey.isEmpty() ?
- '' : grails.util.Holders.config.google.apiKey
- }
- return _googleApiKey
- }
- public static Object getAddressBoundingBox(String address) {
- return getResult(address).results.geometry.bounds[0]
- }
- public static String getIpBasedLocation() {
- def request = WebUtils.retrieveGrailsWebRequest().request
- def isLocal = request.remoteAddr.toString().equals("127.0.0.1") || request.remoteAddr.toString().startsWith("0:0")
- def location = isLocal ? getLookupService().getLocation("178.222.107.131") : getLookupService().getLocation(request.remoteAddr)
- def sRegion = location != null && location.region != null && location.region != "00" ? "${location.region}" : "",
- sCity = location != null && location.city != null ? "${location.city}," : "",
- sCountry = location != null && location.countryName != null ? location.countryName : "",
- ipLocation = "${sCity} ${sRegion} ${sCountry}"
- return ipLocation
- }
- public static Object getLatLng(Location loc) {
- def location = null
- loc.stringVariations().each { String variation ->
- if (location != null) {
- return
- }
- log.debug("Geocoding '${variation}'")
- def result = getResult(variation).results.geometry.location
- log.debug("Result:" + result)
- if (result.size() > 0) {
- location = [lat: result.lat[0], lng: result.lng[0]]
- }
- }
- return location;
- }
- public static Object getLatLng(String address) {
- log.debug("Geocoding '${address}'")
- def result = getResult(address).results.geometry.location
- return [lat: result.lat[0], lng: result.lng[0]]
- }
- public static getResult(String query) {
- def rval = null
- try{
- new HTTPBuilder(GEOCODING_API_HOST).get([path: '/maps/api/geocode/json', query: [key : getGoolgeApiKey(), address: query, sensor: false]]) { resp, json ->
- rval = json
- }
- }catch(ClientProtocolException ex){
- log.error("Protocol error while connecting to ${GEOCODING_API_HOST}",ex)
- }catch(IOException ex){
- log.error("I/O error while connecting to ${GEOCODING_API_HOST}",ex)
- }catch(URISyntaxException ex){
- log.error("URI mallformed: ${GEOCODING_API_HOST}")
- }
- log.debug("Geocoding result:" + rval.toString())
- return rval
- }
- public static getStateAndCountry(String query) {
- def rval = [:]
- getResult(query).results?.address_components[0].each {
- if (it.types.contains('country')) {
- rval['country'] = it.long_name
- }
- if (it.types.contains('administrative_area_level_1')) {
- rval['state'] = it.long_name
- }
- }
- return rval
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement