/
GeocodingParser.groovy
30 lines (25 loc) · 1.02 KB
/
GeocodingParser.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package geofx
import groovy.json.*
class GeocodingParser {
static def GEOCODE_JSON_URL = "http://maps.googleapis.com/maps/api/geocode/json"
static def GEOCODE_XML_URL = "http://maps.googleapis.com/maps/api/geocode/xml"
static def getGeocodeForAddress(address){
def queryBuilder = []
queryBuilder << "address=${URLEncoder.encode(address)}"
queryBuilder << "sensor=false"
def queryString = queryBuilder.join("&")
def requestUrl = GEOCODE_JSON_URL+"?${queryString}"
def payload = new URL(requestUrl).text
def jsonSlurper = new JsonSlurper()
def doc = jsonSlurper.parseText(payload)
def geocode = new Geocode()
geocode.latitude = doc.results.geometry.location.lat.join("")
geocode.longitude = doc.results.geometry.location.lng.join("")
geocode.locationType = doc.results.geometry.location_type.join("")
return geocode
}
}