[問題] goole map api範例的問題

看板Ajax作者 (艾迪達)時間14年前 (2011/11/06 13:46), 編輯推噓1(105)
留言6則, 1人參與, 最新討論串1/1
大家好 我用修改官方的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="" rel="nofollow">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)

11/06 15:50, , 1F
geocoder.geocode 這段 會先連到google去 取得資料後才
11/06 15:50, 1F

11/06 15:50, , 2F
會alert temp+'1'
11/06 15:50, 2F

11/06 15:50, , 3F
所以一定會比alert(temp+'2')慢
11/06 15:50, 3F

11/06 15:51, , 4F
也一定比alert(temp+'3')慢...
11/06 15:51, 4F

11/06 15:51, , 5F
你應該把alert(temp+'2')還有3放到傳給geocode的
11/06 15:51, 5F

11/06 15:52, , 6F
callback function裡面
11/06 15:52, 6F
文章代碼(AID): #1EjXzFTH (Ajax)
文章代碼(AID): #1EjXzFTH (Ajax)