在JSP页面中映射加载和消失

时间:2014-12-08 06:44:02

标签: javascript google-maps-api-3 w3c-geolocation

我在jsp中使用google api编写了一个示例代码,在jsp页面上显示印度地图。问题是,一旦页面加载,地图就会显示,但几秒后就会消失。我无法找到错误。请帮忙

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%   
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
      <head>
        <base href="<%=basePath%>">
       <title>My JSP 'index.jsp' starting page</title>           
        <!-- <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="-1">     -->
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">             
        <!--<link rel="stylesheet" type="text/css" href="styles.css">-->
    <script src="http://maps.google.com/maps?file=api&v=2&key=YOURKEYHERE">
    </script>
    <body>
    <p><strong>locations in India</strong></p>
    <div id="map" style="width: 800px; height: 600px"></div>         
    <script type="text/javascript">                   
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.setCenter(new GLatLng(23.402800, 78.454100), 5, G_NORMAL_MAP);
    function createMarker(point, number)
    {
    var marker = new GMarker(point);
    var html = number;
    GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
    return marker;
    };
    <%
        String userName = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/test";
        try{
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver).newInstance();
            Connection conn;
            conn = DriverManager.getConnection(url, "root","root");
            Statement s = conn.createStatement ();
            s.executeQuery ("SELECT *from latandlon1");
            ResultSet rs = s.getResultSet ();
            int count = 0;
            while (rs.next ()) {
                String lat = rs.getString ("lat");
                String lon = rs.getString ("lon");
                String address=rs.getString ("address");
                out.print("var point = new GLatLng(" + lat + "," + lon +");\n");
                out.print("var marker = createMarker(point,'" + address + "');\n");
                out.print("map.addOverlay(marker);\n");
                out.print("\n");
            }
            conn.close();
            rs.close ();
            s.close ();
        }
        catch(Exception ee){
            System.out.println(ee.toString());  
        }
    %>        
    </script>
   </body>
  </html>

加载页面,页面源如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://localhost:8999/servletapplication/">
   <title>My JSP 'index.jsp' starting page</title>       
    <!-- <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="-1">     -->
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">         
    <!--<link rel="stylesheet" type="text/css" href="styles.css">-->
<script src="http://maps.google.com/maps?file=api&=2&key=YOURKEYHERE">
</script>
<body>
<p><strong>locations in India</strong></p>
<div id="map" style="width: 800px; height: 600px"></div>    
<script type="text/javascript">               
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
map.setCenter(new GLatLng(23.402800, 78.454100), 5, G_NORMAL_MAP);
function createMarker(point, number)
{
var marker = new GMarker(point);
var html = number;
GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
return marker;
};
var point = new GLatLng(23.402800,78.454100);
var marker = createMarker(point,'madhyapradesh');
map.addOverlay(marker);    
var point = new GLatLng(26.280000,80.210000);
var marker = createMarker(point,'kanpur');
map.addOverlay(marker);    
var point = new GLatLng(31.122027,77.111664);
var marker = createMarker(point,'himachal pradesh');
map.addOverlay(marker);    
var point = new GLatLng(22.533000,88.367000);
var marker = createMarker(point,'kolkatha');
map.addOverlay(marker);    
var point = new GLatLng(33.450000,76.240000);
var marker = createMarker(point,'jammu and kashmir');
map.addOverlay(marker);    
var point = new GLatLng(16.508000,80.641700);
var marker = createMarker(point,'vijaywada');
map.addOverlay(marker);    
  </script>
 </body>
</html>

0 个答案:

没有答案