1

我有一个为学校项目编写的 jsp 页面,我使用 jquery mobile 和 js 代码来显示业务位置....
我页面中的问题是,当控制器 servlet 指向时,用于显示地图的画布未显示那个页面。刷新后画布显示完全正常。有人可以帮我理解我做错了什么吗?
编码:

   <!DOCTYPE HTML>  
<%@page import="org.softwarelabs.mosheudi.hit.j2ee.db.Coupon"%>
<HTML>  
<head>  
<meta charset="UTF-8"> 
<title>coupon site</title> 
<% Coupon presentedCoupon = (Coupon)request.getAttribute("Requested coupon");
boolean addCalling = true;%> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>

    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>

<script type="text/javascript">
    if (screen.width > 699) {
        <%addCalling = false; %>
}</script>
<script>

var geocoder;
var map;
function initialize(myAddress) {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var myOptions = {
    zoom: 15,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  geocoder.geocode( { 'address': myAddress}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
}
function codeAddress(myAddress) 
{ 
  geocoder.geocode( { 'address': myAddress}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
</script>

</head>
<body>

<div data-role="page" id="couponDetails" data-add-back-btn="true" style="background-color: #00B2EE"> 

    <div data-role="header">  
        <h1> Coupon information </h1>  

    </div> 

     <div data-role="content" data-inset="true" style="background-color: #00B2EE">

        <div class="ui-grid-b" id="couponInfo">

            <div class="ui-block-a" id="CtextDetails">  
            <h1> Coupon details </h1><br>
                <ul>
                    <h3> <%= presentedCoupon.getM_CouponName() %> </h3><br>
                    <p><strong> Price: <%= presentedCoupon.getM_CouponPrice() %>  </strong></p>
                    <p><strong> Expiration date:&nbsp;&nbsp; <%= presentedCoupon.getM_ExpireDate() %> </strong></p>
                </ul>
            </div>
            <div class="ui-block-b" id="couponImg">  
                <p><img src="<%=(presentedCoupon.getM_imgUrl() != null)?presentedCoupon.getM_imgUrl():"Images/pictures/question_mark.png" %>" alt="coupon photo" style="width: 80%"/></p>
            </div>
            <div class="ui-block-c" >
            <a href="#"  data-theme="e" ><img align="right" align="bottom" alt="buy" src="Images/pictures/buy-now.png" style="width: 40%"></a>
            </div>
        </div> <!-- couponInfo-->

                <img src="Images/pictures/line.png" alt="coupon photo" style="width: 100%"/>

        <div class="ui-grid-a" id="businessInfo">
            <div class="ui-block-a" id="BtextDetails">
            <h1> Business information </h1>  
                <ul>
                    <p><strong> <%=presentedCoupon.getM_Business().getBusinessName() %> </strong></p>
                    <p> <%=presentedCoupon.getM_Business().getM_BusinessAddress() %> </p>
                    <p>  Phone number : <%=presentedCoupon.getM_Business().getM_BusinessPhone() %></p>
                    <%if(addCalling){ %>
                    <a href="<%=presentedCoupon.getM_Business().getM_BusinessPhone() %>" data-mini="true" data-role="button" data-icon="info"> Call </a>
                    <%} %>
                    <div id="map_canvas" style="width: 300px; height: 300px;" onload="initialize('<%=presentedCoupon.getM_Business().getM_BusinessAddress()%>')"></div>
                </ul>
            </div>
            <div class="ui-block-b" id="businessImg">  
                <p><img src="<%=presentedCoupon.getM_Business().getM_imgUrl() %>" alt="business Image" style="width: 80%"/></p>
                <% if(addCalling){ %>
                <a href="useGPSHere"  data-role="button" rel="external" data-icon="search"> Navigate </a>
                <%}else{ %>
                <a href="https://maps.google.co.il/maps"  data-role="button" rel="external" data-icon="search"> Navigate </a>
                <%} %>
            </div>
        </div>
    </div><!-- content-->
</div>
<script>
    initialize('<%=presentedCoupon.getM_Business().getM_BusinessAddress()%>');
    </script>

</body>
</HTML>
4

1 回答 1

1

可能是加载问题,您是否尝试过将您的 google maps api 包含在您的头部的最顶部?顺便说一句,您不应该使用元素事件处理程序;而是退回到 jquery 方式,例如使用 jQuery(document).ready() 函数来监听页面的 onload 事件,如下所示:

   <!DOCTYPE HTML>  
<%@page import="org.softwarelabs.mosheudi.hit.j2ee.db.Coupon"%>
<HTML>  
<head>  
<meta charset="UTF-8"> 
<title>coupon site</title> 
<% Coupon presentedCoupon = (Coupon)request.getAttribute("Requested coupon");
boolean addCalling = true;%> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>

<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 300px; width: 300px; }
</style>

<script type="text/javascript">
    if (screen.width > 699) {
      <%var addCalling = false; %> //declare your variables with 'var'
    }

var geocoder;
var map;
function initialize(myAddress) {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var myOptions = {
    zoom: 15,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  geocoder.geocode({ 'address': myAddress}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
}
function codeAddress(myAddress) { 
  geocoder.geocode( { 'address': myAddress}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
jQuery(document).ready(function() {
    var myAddress = '<%=presentedCoupon.getM_Business().getM_BusinessAddress()%>';
    initialize(myAddress); // call your init function from here
});
</script>

</head>

Since there is no inline css style I suppose you were just setting the map_canvas size directly on the element by mistake (your style in the head says width 100%). You should put that in your head style as well. You can also remove the unnecessary closing/opening script tag before var geocoder.

于 2012-07-02T06:47:57.427 回答