使用jsp为谷歌地图填充从数据库中检索到的值的javascript数组

时间:2013-05-11 04:33:14

标签: javascript jsp google-maps-api-3

您好我正在尝试从数据库中检索IP地址并将其转换为经纬度正常的纬度和经度。我想将它们发送到一个javascript数组并在谷歌地图上绘制它们,我发现它很难做到这一点。我只是在谷歌地图中绘制最后一个值的代码。如何将所有值发送到数组?

  <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ page
        import="javaQuery.j2ee.*" 
        import="javaQuery.importClass.javaQueryBundle"
        import="com.ayu.UI.ConnectionManager"
        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>
    <script src="http://maps.google.com/maps/api/js?sensor=false" 
              type="text/javascript"></script>
    </head>
    <body>
    <%! 
    Connection con = null; 
    ResultSet rs = null; 
    Statement stmt = null;
    GeoLocation $gl;
    String ipaddress = null;
    Double lat;
    Double lon;
    %>
    <%
    String query="select * from iptable";
    con = ConnectionManager.getConnection();
    try {
        stmt=con.createStatement();
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        request.getSession().invalidate();
        e1.printStackTrace();
    }
    try {
        rs = stmt.executeQuery(query);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        request.getSession().invalidate();
        e.printStackTrace();
    }
    %>
    <%
    while(rs.next())
    {

        try {


            ipaddress = rs.getString("ipaddress");
            request.getHeader("VIA");
            $gl = javaQueryBundle.createGeoLocation();
            $gl.MAPTargetByIP(ipaddress, "");
            lat = Double.parseDouble($gl.Latitude);
            lon = Double.parseDouble($gl.Longitude);
        } catch (SQLException e) {
            request.getSession().invalidate();
            e.printStackTrace();
        }
    }
    %>
    <div id="map" style="width: 500px; height: 400px;"></div>

      <script type="text/javascript">
        var locations = [
          ['<%=$gl.City%> ', <%=lat%>, <%=lon%>]
        ];

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 11,
          center: new google.maps.LatLng(<%=lat%>, <%=lon%>),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker;
        var 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>
    </body>
    </html>

0 个答案:

没有答案