Integrating Maps into Your Java Web Application with Google Maps and Ajax

John Ferguson Smart has written a great tutorial about how to integrate the Google Maps API with Java Servlets. He starts off with the usual introduction to Ajax, but soon goes on to more interesting topics, and after just a few lines he already has a fully functional Map in his application.
 
function load() {
   if (GBrowserIsCompatible()) {
     var map = new GMap2(document.getElementById("map"));
     map.setCenter(new GLatLng(-41.5, -187.5), 5);
     map.addControl(new GSmallMapControl());
     map.addControl(new GOverviewMapControl());
   }
 }
Details are then added a few at a time, and what he ends up with is a fully fledged Maps Application, with asynchronous fetching of data and detail Overlays.
 
function processSiteData(xmlDoc){
  // obtain the array of markers and loop through it
  siteMarkers = xmlDoc.documentElement.getElementsByTagName("marker");
  displaySitesMarkers();
}
function displaySitesMarkers() {
   map.clearOverlays();
   for (var i = 0; i < siteMarkers.length; i++) {
     // obtain the attributes of each marker
     var lat = parseFloat(siteMarkers[i].getElementsByTagName("latitude")[0].firstChild.nodeValue);
     var lng = parseFloat(siteMarkers[i].getElementsByTagName("longitude")[0].firstChild.nodeValue);    
     var id = siteMarkers[i].getElementsByTagName("id")[0].firstChild.nodeValue;
     var label = siteMarkers[i].getElementsByTagName("name")[0].firstChild.nodeValue;    
     createSiteMarker(new GLatLng(lat,lng),label,id);
   }
}
All is explained step by step and is really easy to follow, if only all tutorials were this easy :)
Google Maps in action
P.S.: I like people that aren’t too shy to add some humour to their posts:
Of course, the advantage of storing geographical site coordinates in a database is that they can be updated as necessary; for example, in the case of a major earthquake or land slide, or continental drift, or to keep track of the famous migrating mud-transporting camel caravans of the central Elbonian steppes.
Did you like this? Share it:

Leave a Reply