Dynamic Data for Google maps using Java

Here is how to use google maps with dynamic data in a JSF application. Could be easily modified for Struts …etc…
The java classes used:
The POJO

package com.mattiz.web.managedBeans;
public class GoogleMapBean {

	private double latitude;
	private double longitude;
	private String location;

	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
}

The backing bean

package com.mattiz.web.managedBeans;
import java.util.ArrayList;
import java.util.List;
public class GoogleDataLoadBean {

	private List<GoogleMapBean> googleMapLocations;

	private double centerLatitude;

	private double centerLongitude;

	public String populateGoogleMapLocations() {
		List<GoogleMapBean> googleMapLocations = new ArrayList<GoogleMapBean>();
		GoogleMapBean googleMapBean1 = new GoogleMapBean();
		googleMapBean1.setLatitude(-33.890542);
		googleMapBean1.setLongitude(151.274856);
		googleMapBean1.setLocation("Bondi Beach");
		GoogleMapBean googleMapBean2 = new GoogleMapBean();
		googleMapBean2.setLatitude(-33.923036);
		googleMapBean2.setLongitude(151.259052);
		googleMapBean2.setLocation("Coogee Beach");
		GoogleMapBean googleMapBean3 = new GoogleMapBean();
		googleMapBean3.setLatitude(-34.028249);
		googleMapBean3.setLongitude(151.157507);
		googleMapBean3.setLocation("Cronulla Beach");
		GoogleMapBean googleMapBean4 = new GoogleMapBean();
		googleMapBean4.setLatitude(-33.80010128657071);
		googleMapBean4.setLongitude(151.28747820854187);
		googleMapBean4.setLocation("Manly Beach");
		GoogleMapBean googleMapBean5 = new GoogleMapBean();
		googleMapBean5.setLatitude(-33.950198);
		googleMapBean5.setLongitude(151.259302);
		googleMapBean5.setLocation("Maroubra Beach");
		googleMapLocations.add(googleMapBean1);
		googleMapLocations.add(googleMapBean2);
		googleMapLocations.add(googleMapBean3);
		googleMapLocations.add(googleMapBean4);
		googleMapLocations.add(googleMapBean5);
		List<Double> latitudeList = new ArrayList<Double>();
		List<Double> longitudeList = new ArrayList<Double>();
		for(GoogleMapBean g: googleMapLocations){
			latitudeList.add(g.getLatitude());
			longitudeList.add(g.getLongitude());
		}
		setCenterLatitude(findAverage(latitudeList));
		setCenterLongitude(findAverage(longitudeList));
		setGoogleMapLocations(googleMapLocations);
		return "googleMapView";
	}
	public List<GoogleMapBean> getGoogleMapLocations() {
		return googleMapLocations;
	}
	public void setGoogleMapLocations(List<GoogleMapBean> googleMapLocations) {
		this.googleMapLocations = googleMapLocations;
	}
	public static double findAverage(List<Double> a){
		double sum = 0;
		for (double num : a)
		sum += num;
		return sum / a.size();
		}

	public double getCenterLatitude() {
		return centerLatitude;
	}
	public void setCenterLatitude(double centerLatitude) {
		this.centerLatitude = centerLatitude;
	}
	public double getCenterLongitude() {
		return centerLongitude;
	}
	public void setCenterLongitude(double centerLongitude) {
		this.centerLongitude = centerLongitude;
	}
}

The jsp page: mapview.jsp

<?xml version="1.0" encoding="ISO-8859-1"?>
<xmlns="http://java.sun.com/xml/ns/j2ee"
   xsi="http://www.w3.org/2001/XMLSchema-instance"
   schemalocation="http://java.sun.com/xml/ns/j2ee   
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<%@page contentType="text/html"%>
<%@ taglib uri="/WEB-INF/html_basic.tld" prefix="h"%>
<%@ taglib uri="/WEB-INF/jsf_core.tld" prefix="f"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false"
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>
 
  <script type="text/javascript">
 var locations = [
  <c:forEach items="${loadBean.googleMapLocations}" var="googleLocation"  varStatus ="status">
  ['${googleLocation.location}','${googleLocation.latitude}','${googleLocation.longitude}']
    <c:if test="${!status.last}">    
        ,    
    </c:if>
  </c:forEach>
  ]
    alert(locations);
       var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng("${loadBean.centerLatitude}", "${loadBean.centerLongitude}"),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
 
    var infowindow = new google.maps.InfoWindow();
 
    var marker, i;
 
    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });
 
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>
  <table>
    <c:forEach var="googleLocation" items="${loadBean.googleMapLocations}">
        <tr><td>${googleLocation.latitude}<td><td>${googleLocation.longitude}</td></tr>  
    </c:forEach>
</table>
CENTER<tr><td>${loadBean.centerLatitude}<td><td>${loadBean.centerLongitude}</td></tr>
</body>
</html>

The index jsp:

<%@page contentType="text/html"%>
<%@ taglib uri="/WEB-INF/html_basic.tld" prefix="h"%>
<%@ taglib uri="/WEB-INF/jsf_core.tld" prefix="f"%>
<html>
    <head>
        <title>author.jsp</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
    </head>
    <body>
        <f:view>
            <h:form>
                <h:commandButton value="Load Data" type="submit" action="#{loadBean.populateGoogleMapLocations}" />
            </h:form>
        </f:view>
    </body>
</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
 
<faces-config>
<navigation-rule>
        <navigation-case>
            <from-outcome>googleMapView</from-outcome>
            <to-view-id>/pages/mapview.jsp</to-view-id>
        </navigation-case>
</navigation-rule>
 
    <managed-bean>
        <description>JSF Backing Bean for populating Google Data</description>
        <managed-bean-name>loadBean</managed-bean-name>
        <managed-bean-class>com.mattiz.web.managedBeans.GoogleDataLoadBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <description>JSF Backing Bean for google map view</description>
        <managed-bean-name>googleMapBean</managed-bean-name>
        <managed-bean-class>com.mattiz.web.managedBeans.GoogleMapBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
</web-app>

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Mattiz" default="clean" basedir=".">
    <property environment="env" />
    <property name="build.dir" location="build" />
    <property name="build.classes.dir" location="${build.dir}/classes" />
    <property name="build.dist.dir" location="${build.dir}/dist" />
    <property name="java.src.dir" location="src/java" />
    <property name="web.src.dir" location="src/web/mattiz" />
    <property name="JBOSS_HOME" location="H:\mattiz_soft\Jboss501GAJDK6\jboss-5.0.1.GA" />
    <property name="install.jboss.deploy.dir" location="${JBOSS_HOME}/server/default/deploy" />
    <property name="jsfJars.dir" location="lib/jsfJars" />
    <path id="build.path">
        <pathelement location="${JBOSS_HOME}/server/default/lib/jboss-j2ee.jar" />
        <pathelement location="H:\mattiz_soft\tomcat6013\apache-tomcat-6.0.13\lib\servlet-api.jar" />
        <pathelement location="${jsfJars.dir}/jsf-api.jar" />
    </path>
    <target name="clean" depends="install">
        <delete file="${build.dist.dir}/mattiz.jar" />
        <delete file="${build.dist.dir}/mattiz.war" />
        <delete dir="${build.classes.dir}" />
    </target>
    <target name="init">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.classes.dir}" />
        <mkdir dir="${build.dist.dir}" />
    </target>
    <target name="compile" depends="init">
        <javac srcdir="${java.src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" classpathref="build.path" optimize="off" />
    </target>
    <target name="jar" depends="compile">
        <jar destfile="${build.dist.dir}/mattiz.jar">
            <fileset dir="${build.classes.dir}" includes="**/*" excludes="**/*.java" />
        </jar>
        <copy todir="${build.classes.dir}">
            <fileset dir="${java.src.dir}" />
        </copy>
        <war destfile="${build.dist.dir}/mattiz.war" webxml="WebRoot/WEB-INF/web.xml">
            <webinf dir="WebRoot/WEB-INF" />
            <lib dir="${jsfJars.dir}" />
            <lib dir="${build.dist.dir}" includes="mattiz.jar" />
            <zipfileset dir="${web.src.dir}/pages" prefix="pages" />
        </war>
    </target>
    <target name="install" depends="jar">
        <copy overwrite="${FORCE}" todir="${install.jboss.deploy.dir}">
            <fileset dir="${build.dist.dir}">
                <include name="mattiz.war" />
            </fileset>
        </copy>
    </target>
</project>

Resource structure:


The URL to hit:
http://localhost:8080/mattiz/pages/index.faces
The output: