我有一个为学校项目写的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: <%= 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>
答案 0 :(得分:1)
可能是一个加载问题,你试过把你的谷歌地图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>
由于没有内联css样式,我认为你只是错误地在元素上直接设置map_canvas大小(头部的样式表示宽度为100%)。你也应该把它放在你的头脑风格中。您还可以在var geocoder
之前删除不必要的结束/打开脚本标记。