0

我試圖從該圖「模擬」的代碼:簡單地址解析器

https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

僅改變Syney到阿雷格里港(形式值和座標以及)。但是由於地圖畫布沒有顯示在屏幕上,所以出現了問題。我檢查了代碼,沒有發現任何錯字或語法錯誤。

我希望有人能幫忙。實際上,我比Google Maps API更多地使用Fusion Tables。

<!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>GEOCODER</title> 
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
<script src="https://maps.google.com/maps/api/js?sensor=false"></script> 
<script> 

var geocoder; 
var map; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-30.027704, -51,228735); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 

function codeAddress() { 
    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     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> 
</head> 
<body onload="initialize()"> 
<div id="map_canvas" style="width: 320px; height: 480px;"></div> 
<div> 
    <input id="address" type="textbox" value="Porto Alegre, Brasil"> 
    <input type="button" value="Encode" onclick="codeAddress()"> 
</div> 
</body> 
</html>  

回答

2

您可能交換了兩行,導致語法錯誤。如果你想檢查你的JavaScript控制檯,你會看到錯誤信息。如果JS沒有達到你期望的目標,那麼你應該這樣做(或者,對於這一點,幾乎總是這樣做,只是爲了確保沒有任何意外的錯誤)。

如果你有這樣的:

 } 
    ); 
    } 
} 
</script> 

你想這樣的:

 } 
    } 
); 
} 
</script> 
+0

非常感謝,男人!現在地圖工作正常!我認爲使用'代碼編輯器'就足夠了。 JavaScript控制檯將是Firebug? – 2012-07-31 01:58:00

+0

Firebug提供訪問控制檯,是的。但是每個現代瀏覽器都內置了一個瀏覽器。確實值得研究如何看待它。這對調試至關重要。 – Trott 2012-07-31 02:55:09

0

請注意,您在您的標題提供一個相對鏈接到CSS文件。這適用於谷歌的服務器,但可能不會在你的服務器上,除非你創建了相同的相對路徑。試試這個:

<link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
+0

那麼,代碼已與該鏈接工作,但我會嘗試檢查與調試器。謝謝! – 2012-07-31 22:20:27