我有以下谷歌地图代码:
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-27.444916,153.03),
zoom: 12,
disableDefaultUI: true,
keyboardShortcuts: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
setMarkers(map, beaches);
}
var beaches = [
['Car:806', -27.4481025666613, 153.035155217002, 916],
['Car:520', -27.4777186625335, 153.00697421394, 915],
['Car:1477', -27.4523199466005, 153.029570366226, 914],
['Car:815', -27.5315143182514, 153.023819055977, 913],
['Car:334', -27.3930054770139, 153.104887341157, 912],
['Car:191', -27.4705744176833, 153.030243260862, 911],
...
['Car:618', -27.4146792620242, 153.081801794924, 1],
['Car:1096', -27.3369540394594, 153.069279763313, 0],
];
function setMarkers(map, locations) {
var image = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_orange.png',
new google.maps.Size(20, 32),
new google.maps.Point(0,0),
new google.maps.Point(0, 32));
var shadow = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_shadow.png',
new google.maps.Size(37, 32),
new google.maps.Point(0,0),
new google.maps.Point(0, 32));
var shape = {
coord: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
for (var i = 0; i < locations.length; i++) {
var beach = locations[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
shadow: shadow,
icon: image,
shape: shape,
title: beach[0],
zIndex: beach[3]
});
}
}
</script>
它在chrome和IE中运行良好,但在IE中我得到一个错误。这一切对我来说都很好,但我很想知道为什么我会收到这个错误:
网页错误详情
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.4; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322)
Timestamp: Wed, 30 Jan 2013 22:40:09 UTC
Message: '1' is null or not an object
Line: 966
Char: 1
Code: 0
URI: file://ycab01/Homedirs$/it3/Desktop/Michael/WEB%20DEV/CarPositionBrisbane.HTML
对第996行的引用是:
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
我认为IE不喜欢调用数组中的索引。我只是想知道是否有不同的方法来做到这一点?
麦克
答案 0 :(得分:1)
IE不喜欢“悬挂逗号”(数组或对象末尾的逗号,后面没有任何内容)。它们导致它在对象或数组的末尾附加一个null,然后在API中生成错误。
从代码中删除它们。
var beaches = [
['Car:806', -27.4481025666613, 153.035155217002, 916],
['Car:520', -27.4777186625335, 153.00697421394, 915],
['Car:1477', -27.4523199466005, 153.029570366226, 914],
['Car:815', -27.5315143182514, 153.023819055977, 913],
['Car:334', -27.3930054770139, 153.104887341157, 912],
['Car:191', -27.4705744176833, 153.030243260862, 911],
...
['Car:618', -27.4146792620242, 153.081801794924, 1],
['Car:1096', -27.3369540394594, 153.069279763313, 0], // ** remove the comma at the end of this line **
];