gpt4 book ai didi

javascript - 除非刷新,否则谷歌地图无法正确显示

转载 作者:搜寻专家 更新时间:2023-11-01 04:57:31 24 4
gpt4 key购买 nike

我将 JQuery Mobile 与 Google Map API 结合使用。除非我刷新页面,否则 map 无法正确显示,但我不明白为什么。

这是我的 map.html 文件:

<div data-role="page" id="map-page">  
<div data-role="content" id="canvas-map" style="background-color:red"></div>
<script src="js/map.js"></script>
<script type="text/javascript">
var $canvasMap = $('#canvas-map');
$('#canvas-map').on('pagebeforeshow', initMap());
</script>
</div>

和我的 map.js 文件:

function initMap() {
"use strict";
google.maps.visualRefresh = true;

var marker, map,
myLocation = {lat:50, lon:-80},
mapOptions = {
zoom: 5,
center: new google.maps.LatLng(myLocation.lat, myLocation.lon),
mapTypeId: google.maps.MapTypeId.PLAN,
disableDefaultUI: true
};

$('#canvas-map').css("height", "200px").css("padding", "0px");
map = new google.maps.Map(document.getElementById('canvas-map'), mapOptions);

/** MyPosition **/
marker = new google.maps.Marker({
map: map,
draggable: false,
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng(myLocation.lat, myLocation.lon),
});
}

如果我从另一个页面导航到上面的页面,我会得到以下结果: broken google maps

然后当我刷新时,我得到了预期的结果: refreshed google maps working fine

这显然不是 Canvas 的问题,因为它显示为(红色)。另外,如果我浏览 map ,我可以看到标记显示在正确的位置。这些屏幕截图是使用 Google Chrome 制作的,我用 Firefox 试过,这里的 Canvas 完全是红色的,根本没有 map 。

PS:这是我原始代码的一个简单版本,但行为是一样的。

编辑:

有关详细信息,请参阅 Gajotres 的回答,但基本上,在访问 map.html 页面的链接中,添加 target="_blank" 解决了问题。请注意,target="_self" 似乎也可以正常工作,这很奇怪,因为它应该是默认值。 目标的详细信息here .

最佳答案

google maps v3 框架可以通过 jQuery Mobile 轻松实现。

下面说说这里的问题。您的内容 div 存在高度和宽度问题,主要是因为只有在 pageshow 事件期间才能正确计算页面尺寸。但即便如此,内容 div 也不会覆盖整个页面。

你的问题可以用一点 CSS 来解决:

#content {
padding: 0 !important;
position : absolute !important;
top : 40px !important;
right : 0 !important;
left : 0 !important;
height: 200px !important;
}

基本上,您是在强制您的内容覆盖可用空间。

工作示例:http://jsfiddle.net/RFNPt/2/

最终解决方案:

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<title>MyTitle</title>
</head>
<body>
<div data-role="page" class="page">
<div data-role="content" id="index-content">
<div id="menu">
<a href="map.html" data-role="button" target="_blank">Map</a>
</div>
</div>
</div>
</body>
</html>

map .html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<title>MyTitle</title>
</head>
<body>
<div data-role="page" id="map-page">
<div data-role="content" id="content">
<div id="canvas-map" style="height:100%"/>
</div>
<style>
#content {
padding: 0 !important;
position : absolute !important;
top : 0 !important;
right : 0 !important;
bottom : 40px !important;
left : 0 !important;
}
</style>
<script type="text/javascript">
$(document).on('pageshow', '#map-page', function(e, data) {
var marker, map,
myLocation = {
lat: 50,
lon: -80
},
mapOptions = {
zoom: 5,
mapTypeId: google.maps.MapTypeId.PLAN,
center: new google.maps.LatLng(myLocation.lat, myLocation.lon)
};
$('#canvas-map').css("height", "200px").css("padding", "0px");
map = new google.maps.Map(document.getElementById('canvas-map'), mapOptions);
marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(myLocation.lat, myLocation.lon),
});
});
</script>
</div>
</body>
</html>

如果你看一下这个 ARTICLE 您将找到有关此主题的更多信息以及示例。

关于javascript - 除非刷新,否则谷歌地图无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com