我从 Google Maps JavaScript API V3 站点获取了基本地图演示,并将其放置在一个名为“testnav.htm”的页面中。我正在尝试使用 JQM 将此页面作为对话框打开,但该对话框无法正确显示。我得到的只是对话框标题。我的 testnav.htm 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 200px;
width: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
console.log('here');
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div data-role="dialog">
<div data-role="header" data-theme="d">
<h1>Map</h1>
</div>
<div data-role="content" data-theme="c">
<div id="map-canvas"></div>
</div>
</div>
</body>
</html>
我这样称呼它:
<a href="testnav.htm" data-rel="dialog">View On Map</a>
为什么地图画布不显示?
更新遵循奥马尔的建议,这是我现在正在运行的代码......
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
</head>
<body>
<div data-role="dialog">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
$(document).on('pageshow', '[data-role=dialog]', function () {;
var map;
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
console.log('end');
});
</script>
<div data-role="header" data-theme="d">
<h1>Map</h1>
</div>
<div data-role="content" data-theme="c">
<div id="map-canvas" ></div>
</div>
</div>
</body>
但我不确定在哪里添加google.maps.event.addDomListener(window, 'load', initialize);