Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

A Google Maps mash-up

Integrate external data with the Google Maps API

  • Print
  • Feedback

Page 2 of 3

You will also need to register for a Google Maps API key (available by following the link to the Google Maps API in Resources) and change the <script> tag to use your key when referencing the Google Maps API in index.htm.

The SOAP client

We start by creating the SOAP call to geocoder.us. Apache Axis's WSDL2Java tool generates our SOAP client using the geocoder.us Web Services Description Language. The basic command is:

 java org.apache.axis.wsdl.WSDL2Java -t -o src -Nhttp://rpc.geocoder.us/Geo/Coder/US/=samples.mapMashup http://geocoder.us/dist/eg/clients/GeoCoder.wsdl


As its name suggests, the WSDL2Java tool inspects geocoder.us's WSDL and generates the Java client binding that can invoke geocoder.us's services using SOAP. For additional information on Apache Axis, please refer to Resources.

GeoCoder proxy bean

Next, we create a GeoCoder proxy bean that uses our Axis-generated SOAP client to invoke the geocoder.us service. We will invoke this class using DWR from our HTML page. So, to satisfy DWR, our proxy bean must be a Java bean allowing introspection:

 public class GeoCoder {
    public GeoCoder() {
    }
   
    public GeocoderAddressResult[] geocode(String location) throws IOException, ServiceException {
        GeoCode_BindingStub binding = (GeoCode_BindingStub) new GeoCode_ServiceLocator().getGeoCode_Port();
   
        // Time out after a minute
        binding.setTimeout(60000);   
        GeocoderAddressResult[] values = binding.geocode_address(location);
        return values;
    }
  
}


DWR: Direct Web Remoting

Our sample application uses Ajax to process the user-entered address into latitude/longitude coordinates. While many Ajax libraries are available, this application uses DWR, which allows us to invoke published server-side bean methods from client-side JavaScript, thus freeing us from writing much of the client-to-server infrastructure code.

DWR's approach to Ajax is to dynamically generate JavaScript code based on JavaBeans components. The dwr.xml configuration file specifies which Java beans to expose for remote use via JavaScript:

 <dwr>
  <allow>
    <create creator="new" javascript="GeoCoder">
      <param name="class" value="samples.mapMash.GeoCoder"/>
    </create>
    <convert converter="bean" match="samples.mapMash.GeocoderAddressResult"/>
  </allow>
</dwr>


The DWR library dynamically creates a usage and test page, which you can use to test the bean methods being exposed. To see this, go to http://localhost:8080/MapMash/dwr/. Visit the page http://localhost:8080/MapMash/dwr/test/GeoCoder to see the DWR-generated JavaScript functions.

The DWR test page generates the script tags that we'll need to include in our HTML page:

 <script type='text/javascript' src='/MapMash/dwr/interface/GeoCoder.js'></script>
<script type='text/javascript' src='/MapMash/dwr/engine.js'></script>


The first library (GeoCoder.js) contains the DWR-generated code to access our GeoCoder.java proxy bean. The contents of this library appear below:

 function GeoCoder() { }
GeoCoder.geocode = function(p0, callback)
{
    DWREngine._execute('/MapMash2/dwr', 'GeoCoder', 'geocode', p0, callback);
}


The sample application's HTML page invokes the GeoCoder.geocode() function, which in turn relies on the DWR engine, referenced above as engine.js. The DWR engine handles the details of invoking our Java class via Ajax—we only need to supply a callback function (described below).

  • Print
  • Feedback

Resources