我有一个使用 Google Maps Javascript API (extPage.php) 的页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var cord = new google.maps.LatLng(28.545078,-81.377196);
var mapOptions = {
zoom: 15,
center: cord,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"style="width:600px; height:500px"></div>
</body>
</html>
该页面按原样加载正常,但我正在尝试加载到另一个页面上的 div 中。我这样做(test.php):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#LoadBut").click(function() {
$("#extDiv").load('extPage.php');
});
});
</script>
</head>
<body>
<input type="button" id="LoadBut" value="Load">
<div id="extDiv"></div>
</body>
</html>
当我像这样运行它时,出现错误 ReferenceError: google is not defined
。我尝试过移动 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
到 test.php 页面。错误消息消失,但 map 未加载。
我从来没有确定过在像这样的 div 内的页面上处理 Javascript 的最佳方法。
对此的任何帮助都会很棒。
谢谢!
最佳答案
根据我对您的要求的了解,您可以使用 Google Static Maps API ,这不需要任何 JavaScript。
您只需使用适当的参数将带有静态 map URL 引用的 img 标签放置到您的页面中即可。举个例子:
<div id="extDiv">
<img src="https://maps.googleapis.com/maps/api/staticmap?center=28.545078,-81.377196&zoom=15&size=600x500&maptype=roadmap&sensor=false" />
</div>
请参阅文档顶部的注释,了解包含的 API key 是否适用于您的项目。
关于javascript - 使用 jQuery load() 在 Div 内的页面上加载 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16407682/