0
我正在使用google maps API進行一些地理編碼。 用戶使用某個地址填寫表單,當點擊提交按鈕時,會調用codeAddress()函數,該函數應該執行地理編碼,但它不起作用。 作爲一個測試,我硬編碼的地址和initialize()中的文檔時調用codeAddress函數。 當頁面準備就緒時調用codeAddress時,它可以正常工作,但是當它由onclick腳本調用時,它不起作用(不會返回由螢火蟲返回的錯誤!)。地理編碼功能在初始化時調用,但不在onclick腳本中調用
有人可以幫忙嗎? 感謝
(你可以將代碼粘貼在這裏:http://code.google.com/apis/ajax/playground/?exp=maps#map_simple,或更改谷歌API密鑰)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps API Sample</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
codeAddress();
}
}
function codeAddress() {
var geocoder = new GClientGeocoder();
var address = "paseo montjuic, 30, barcelona, spain";
geocoder.getLatLng(address, function(point) {
if (point) {
alert(point);
} else {
alert("Geocode was not successful");
}
});
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()" style="font-family: Arial;border: 0 none;">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
<form action="" method="POST">
<!-- stuff -->
<input type="Submit" value="Submit" onclick="codeAddress();" />
</form>
</body>
</html>
但是,如果在onclick中調用codeAddress(),我會看到警報,對不對? – jul 2009-12-04 10:49:27
它會按照您從腳本調用它的方式工作。這意味着你應該看到警報。 – mauris 2009-12-04 11:27:40
好吧,看起來頁面在提交表單時無需等待codeAddress()就可以重新加載。適用於「返回false」。謝謝! – jul 2009-12-04 13:15:41