2011-05-28 63 views
1

我在下面的代碼在IE8 & Firefox有一些問題。谷歌地圖問題與IE8和火狐

出於某種原因,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

把它放在jsfiddle它對我來說很好用:http://jsfiddle.net/9LEKZ/你把一個正確的地址?你確定它存在的地址有錯誤嗎? – Flauwekeul 2011-05-28 16:02:10

回答

1

initialize()由於地圖畫布爲空而導致錯誤。地圖畫布爲空,因爲getDocumentById找不到map_canvasgetDocumentById找不到map_canvas,因爲IE需要<div>位於<body>標記內。

此IE中的腳本對我來說:

<body> 
<div id="map_canvas" style="height: 408px; width: 750px;"></div> 
<script> 
    initialize(); 
    codeAddress(); 
</script> 
</body> 

因此,請確保您的文檔是良好的HTML與<html><head>,並<body>標籤的所有適當安排。