Skip to content

Instantly share code, notes, and snippets.

@ianibo
Created December 11, 2012 16:35
Show Gist options
  • Save ianibo/4260123 to your computer and use it in GitHub Desktop.
Save ianibo/4260123 to your computer and use it in GitHub Desktop.
Groovy scriptlet to reverse geocode a lat/long and extract county and other address information
#!/usr/bin/groovy
@Grapes([
@Grab(group='com.gmongo', module='gmongo', version='0.9.2'),
@Grab(group='org.apache.httpcomponents', module='httpmime', version='4.1.2'),
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.0'),
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0'),
@Grab(group='org.apache.httpcomponents', module='httpmime', version='4.1.2')
])
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import static groovyx.net.http.ContentType.JSON
def http = new HTTPBuilder("http://maps.googleapis.com");
http.request(Method.valueOf("GET"), JSON) {
uri.path = '/maps/api/geocode/json'
uri.query = [ 'latlng' : '53.2281100000,-0.5499100000',
'sensor' : 'false' ]
response.success = {resp, json ->
json.results.each { r ->
r.address_components.each { ac ->
if ( ac.types.contains('administrative_area_level_2') ) {
println("Candidate administrative area: ${ac.long_name} ${ac.short_name}");
}
else {
println("Non county name: ${ac.long_name}");
}
}
}
// Alternatively, just go with the first in the list
if ( json.results.size() > 0 ) {
println("Best guess:");
println(json.results[0]);
}
// json.books.each { book ->
// assert book.name != ""
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment