Java Reference
In-Depth Information
section I'll show that there's actually a class in the Groovy API called Binding , but here
I'll do the binding implicitly through the Java API.
A binding is a collection of variables at a scope that makes them visible inside a script.
In the JSR 223 API, the ScriptEngine class itself acts as a binding. It has both a put
and a get method that can be used to add variables to scripts and retrieve the results from
them.
To illustrate this, let's do something a bit less trivial and possibly more practical. Instead of
doing a simple “Hello, World!” script, consider the Google geocoder, in its version 2 form.
Groovy Sweet Spot
Groovy scripts are an easy way to experiment with new libraries.
A geocoder is an application that converts addresses to latitude/longitude pairs. Google has
hadapubliclyavailable geocoderforyears.InthissectionI'lluseversion2,whichrequires
a key (available through a free registration), but which gives me the chance to show some
interesting Groovy features. When I discuss XML processing later in this chapter I'll use
version3ofthegeocoderinstead.Thatversionnolongerrequiresakey,butitdoesn'tmake
the results available in the same comma-separated form I'll use here.
The documentation for version 2 of the Google geocoder can be found at http://mng.bz/
Pg8S . Version 2 is currently deprecated but still works. I'm using it here because it's famil-
iar from the previous chapter, so you can focus on the input/output parts of the script, and
because it also lets me demonstrate multiple return values. [ 4 ]
4 Another reason to show the version 2 geocoder is because the Google Maps API for Android still uses it.
In order to use the geocoder, the basic idea is to transmit an address as a parameter in an
HTTP GET request and process the results. As shown in chapter 2 , using the Google geo-
coder takes the following steps:
1 . Convertalistcontainingthestreet,city,andstateintoaURL-encodedstringwhose
values are separated by “,”.
 
Search WWH ::




Custom Search