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 3 of 3

Your application console may warn you about missing items related to Spring, Hibernate, Bean Scripting Framework, JDOM, etc.:

 Missing classdef for creator 'spring'. Failed to load uk.ltd.getahead.dwr.create.SpringCreator.
Cause: org/springframework/beans/factory/BeanFactory Missing classdef for creator 'script'.
Failed to load uk.ltd.getahead.dwr.create.ScriptedCreator.
etc..


You can safely ignore these; we are not utilizing these features.

Tying it all together

Finally, in the browser, we display a map and an input field for the user to type an address. The Submit button's onclick event invokes the findAddress_onclick() event-handler function, which calls our GeoCoder proxy via DWR to translate the address into latitude/longitude coordinates and redraws the map accordingly:

 function findAddress_onclick() {
    var address = document.getElementById("address").value;
    // Create an anonymous callback function to manipulate the
    // map using the given latitude/longitude coordinates. 
    var moveMapCallback = function(points) {
        if (points.length > 0) {
            // In case there are multiple matches, we just use the first.
            var point = points[0];
            var newCenter = new GPoint(point._long, point.lat);
            map.recenterOrPanToLatLng(newCenter);
            map.addOverlay(new GMarker(newCenter));
            map.openInfoWindow(newCenter,
               document.createTextNode("Coordinates: " + newCenter)); 
            document.getElementById("debugGeoCoding").innerHTML= newCenter;
       } else {
            document.getElementById("debugGeoCoding").innerHTML = 'no match';
        }
    }
   // DWR will proxy this call to GeoCoder's geocode method on the server.
    // Once it gets a response, DWR will pass the result as an arg to the callback.
    GeoCoder.geocode(address, moveMapCallback);
}


Note that the findAddress_onclick() function passes an anonymous callback function to the DWR-generated GeoCoder.geocode() function. This satisfies the contract with DWR's generated JavaScript; since Ajax is asynchronous, the callback function is invoked once the call to our GeoCode proxy is completed. The geocode() method in GeoCoder.java returns an array of GeocoderAddressResults, which DWR passes to the callback function as an argument. The callback uses the first GeocoderAddressResult in the passed-in array to manipulate the map by centering to the new coordinates, adding a marker at the new center, and adding an info window displaying the coordinates.

Conclusion

While the mash-up demonstrated here may be a bit trivial, the framework used to build it can be applied to develop a much more robust application. We've integrated an outside datasource via SOAP, and we have "remotely" invoked our SOAP service using Ajax via the DWR library. This approach can be easily extended to integrate additional data to Google's map interface or to integrate third-party data into your own application.

About the author

Sumit Bando started his professional career working on compilers and debuggers at AT&T and Apple. He worked on Java virtual machines at Apple and Sun, and has been directing software development on the J2EE and Web services stack for the past six years, most recently at Selectica. Darius Kasad has more than 10 years of experience leading and delivering Web and client-server solutions in both start-up and enterprise environments, and is currently a software engineer at Selectica.

Read more about Tools & Methods in JavaWorld's Tools & Methods section.

  • Print
  • Feedback

Resources