2012-07-01 77 views
1

我有一個jsp頁面爲一個學校項目書寫,即時通訊使用jquery移動和js代碼來顯示一個商業位置....
我的網頁的問題是當控制器servlet指向該頁面時,用於顯示映射的畫布不顯示。刷新後畫布顯示完全正常。有人能幫我理解我做錯了什麼嗎?
代碼:谷歌地圖api不顯示在第一個jsp頁面加載

<!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> 

回答

1

可能是一個加載的問題,你有沒有試圖把你的谷歌地圖API,包括你的頭段的最頂端?順便說一句,你不應該使用元素事件處理程序;而是回到jquery的方式,例如使用jQuery(文件)。就緒()函數來聽你的頁面,像這樣的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> 

由於沒有內嵌CSS樣式,我想你只是直接設置map_canvas提供大小的元素錯誤地(你的頭腦風格說寬度100%)。你應該把它放在你的頭腦風格上。您還可以在var geocoder之前刪除不必要的關閉/打開腳本標記。