Google Maps API v3 - TypeError:表达式'google.maps.LatLng'[undefined]的结果不是构造函数

Ste*_*emp 7 javascript google-maps-api-3

我正在创建一个静态html页面来显示数据中的多个位置.我刚刚复制了其中一个示例并正在向后工作,但我在Safari Inspector中收到以下错误:

main.js:1SyntaxError: Parse error
sample.htm:10TypeError: Result of expression 'google.maps.LatLng' [undefined] is not a constructor.
Run Code Online (Sandbox Code Playgroud)

这是我的HTML代码:

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Multi Markers Sample via Google Maps</title> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
function initialize() {
var myLatlng = new google.maps.LatLng(-30.2965590,153.1152650);
var myLatlng1 = new google.maps.LatLng(-30.2956470,153.1123707);
var myLatlng2 = new google.maps.LatLng(-30.2864430,153.1360230);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
icon: 'http://www.google.com/intl/en_us/mapfiles/ms/icons/blue-dot.png',
title:"Original Location"     });   
var marker = new google.maps.Marker({
position: myLatlng1,
map: map,
title:"Tom Cruise"     }); 
var marker = new google.maps.Marker({
position: myLatlng2,
map: map,
title:"Lady Gaga"     }); 
} 
</script> 
</head> 
<body onLoad="initialize()">   
<div id="map_canvas"></div> 
</body> 
</html>
Run Code Online (Sandbox Code Playgroud)

我不确定我在这里做错了什么 - 它实际上适用于Windows上的IE v8但不适用于Safari,我需要让它适用于两者.

Tha*_*Guy 22

将回调函数添加到您的Google地图请求中.它将在Google加载所需内容后执行.

http://maps.google.com/maps/api/js?sensor=false&callback=initialize
Run Code Online (Sandbox Code Playgroud)

  • +1生命保护伴侣; 这个答案应该被接受! (2认同)