作者twofer (艾迪達)
看板Ajax
標題[問題] goole map api範例的問題
時間Sun Nov 6 13:46:49 2011
大家好
我用修改官方的sample一小部分做測試
但是執行結果的順序跟我想得不太一樣
我想的順序是
---------------
get data 1
get data 2
get data 3
執行結果是
---------------
init 2
init 3
get data 1
請問各位板上高手
該怎麼解決
謝謝
下面為程式碼---------------------------------
<html>
<head>
<title>New Web Project</title>
<script type="text/javascript" src="
http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
<script type="text/javascript">
var geocoder;
var map;
var temp = "init";
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom : 8,
center : latlng,
mapTypeId : google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function codeAddress(address) {
if(geocoder) {
geocoder.geocode({'address' : address }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
temp = "get data";
alert(temp + " 1");
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}alert(temp + " 2");
}
function enter() {
var from = document.getElementById("From").value;
codeAddress(from);
alert(temp+ " 3");
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: 320px; height: 480px;"></div>
<div>
<input id="From" type="textbox" value="Sydney">
<input type="button" value="Encode" onclick="enter()">
</div>
</body>
</html>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.167.221.157
※ 編輯: twofer 來自: 118.167.221.157 (11/06 14:46)
※ 編輯: twofer 來自: 118.167.221.157 (11/06 15:11)
1F:推 mrbigmouth:geocoder.geocode 這段 會先連到google去 取得資料後才 11/06 15:50
2F:→ mrbigmouth:會alert temp+'1' 11/06 15:50
3F:→ mrbigmouth:所以一定會比alert(temp+'2')慢 11/06 15:50
4F:→ mrbigmouth:也一定比alert(temp+'3')慢... 11/06 15:51
5F:→ mrbigmouth:你應該把alert(temp+'2')還有3放到傳給geocode的 11/06 15:51
6F:→ mrbigmouth:callback function裡面 11/06 15:52