我在IE8&中遇到了以下代码的问题。 Firefox浏览器。
出于某种原因IE8错误说
Webpage error details
Message: 'null' is null or not an object
Line: 30
Char: 1472
Code: 0
URI: https://maps-api-ssl.google.com/intl/en_gb/mapfiles/api-3/5/4a/main.js
这是我正在使用的代码......
<link href="https://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-0, 0);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function codeAddress() {
var address = 'ACTUAL ADDRESS GOES HERE';
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latlng = new google.maps.LatLng(results[0].geometry.location);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
<div id="map_canvas" style="height: 408px; width: 750px;"></div>
<script>
initialize();
codeAddress();
</script>
任何人都可以帮助我吗?不知道为什么这在IE和Firefox中不起作用!
答案 0 :(得分:1)
initialize()
导致错误,因为地图画布为空。地图画布为空,因为getDocumentById
找不到map_canvas
。 getDocumentById
无法找到map_canvas
,因为IE需要<div>
位于<body>
标记内。
这为我修复了IE中的脚本:
<body>
<div id="map_canvas" style="height: 408px; width: 750px;"></div>
<script>
initialize();
codeAddress();
</script>
</body>
因此,请确保您的文档格式正确,并且<html>
,<head>
和<body>
标记都已正确排列。