I have to read data from a database and show it as markers on google map.I have done that but i am stucked because my map only displays the last marker from the table.I think the problem is on my javascript var locations array.I have a problem on how to parse data to google map's javascript locations array.Please help me.Here is my source code.
<%@page import="java.util.List"%> <%@page import="java.util.ArrayList"%> <%@page import="cgs.seismicEvents.DBConnection.DBUtility"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="javaQuery.j2ee.*"%> <%@ page import="javaQuery.importClass.javaQueryBundle"%> <%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>GeoLocation</title>
<%! Connection con = null; ResultSet rs = null; Statement stmt = null; //GeoLocation $gl; //String ipaddress = null; float event_Size; double latitude; double longitude; Timestamp event_date; %> <% String query="SELECT EVENTSIZE,LATITUDE,LONGITUDE,EVENTDATE FROM EVENTS where EVENTDATE>=currentdate-30 order by EVENT_DATE desc"; con = DBUtility.getConnection(); try { stmt=con.createStatement(); } catch (SQLException e1) {
request.getSession().invalidate();
e1.printStackTrace();
}
try {
rs = stmt.executeQuery(query);
} catch (SQLException e) {
request.getSession().invalidate();
e.printStackTrace();
}
%>
<%
while(rs.next()) { latitude = rs.getDouble("LATITUDE"); longitude =rs.getDouble("LONGITUDE"); eventSize=rs.getFloat("EVENTSIZE"); eventdate=rs.getTimestamp("EVENTDATE");
System.out.println("var locations = new google.maps.LatLng("+event_Size+","+latitude+","+longitude+","+event_date+");\n");
}
%>
<div id="map" style="width: 1400px; height: 900px;"></div>
<script type="text/javascript">
<%--My problem is here,I want to get values from the resultset and parse them to this array--%>
var locations =[
["<%=event_Size%>","<%=latitude%>","<%=longitude %>","<%=event_date%>]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-25.731340,28.218370),
mapTypeId: google.maps.MapTypeId.HYBRID
});
var infowindow = new google.maps.InfoWindow();
var marker;
var i;
for (i = 0; i < locations.length; i++){
// var data=locations[i];
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1],locations[i][2]),
icon:{
path: google.maps.SymbolPath.CIRCLE,
scale: 4
},
map: map
});
document.write();
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function(){
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
};
})(marker, i));
}