2011-03-09 59 views
15

當我使用下面的代碼提醒空白值?這是爲什麼?如何從Google Maps JavaScript地理編碼器返回經度和緯度?

HTML

<body onload="initialize()"> 
<div id="map_canvas" style="width: 320px; height: 480px;"></div> 
    <div> 
    <input id="address" type="textbox" value="Sydney, NSW"> 
    <input type="button" value="Encode" onclick="display()"> 
    </div> 
</body> 

的JavaScript

var geocoder; 
var map; 


    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() { 
    var address = document.getElementById("address").value; 
    var loc=[]; 

    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     loc[0]=results[0].geometry.location.lat(); 
     loc[1]=results[0].geometry.location.lng(); 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 

    return loc; 
    } 

    function display(){ 
    var long_lat=codeAddress(); 
    alert(long_lat); 
    } 

回答

22

因爲你的函數codeAddress被執行時,分配空數組爲LoC,執行異步請求,以谷歌地址解析器,並返回LOC,裏面是空的,因爲它的真實價值來自谷歌的響應時分配。換句話說,阿勒特應該響應處理程序中:

var geocoder; 
var map; 


    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() { 
    var address = document.getElementById("address").value; 
    var loc=[]; 

    // next line creates asynchronous request 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     // and this is function which processes response 
     if (status == google.maps.GeocoderStatus.OK) { 
     loc[0]=results[0].geometry.location.lat(); 
     loc[1]=results[0].geometry.location.lng(); 

     alert(loc); // the place where loc contains geocoded coordinates 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 

    // pretty meaningless, because it always will be [] 
    // this line is executed right after creating AJAX request, but not after its response comes 
    return loc; 
    } 

    function display(){ 
    codeAddress(); 
    } 

這是AJAX是如何工作的?在回調處理程序處理結果。


,如果你想單獨地理編碼和 'dispalying' 可以執行內部處理顯示功能:

function codeAddress() { 
    var address = document.getElementById("address").value; 

    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     var loc=[]; // no need to define it in outer function now 
     loc[0]=results[0].geometry.location.lat(); 
     loc[1]=results[0].geometry.location.lng(); 

     display(loc); 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 

    } 

    function display(long_lat){ 
    alert(long_lat); 
    } 

HTML:

<input type="button" value="Encode" onclick="codeAddress()"> 


可以使它更加通用,如果你不僅要顯示地理編碼。然後,你可以定義回調作爲參數傳遞給 codeAddress功能:

function codeAddress(callback) { 
... 
geocoder.geocode({ 'address': address}, function(results, status) { 
    ... 
    callback(loc); // instead of dispaly(loc); 
    ... 
} 
... 
} 

codeAddress(display); // to execute geocoding 
+0

謝謝您的回答,有沒有返回這些值的變量的方法嗎? bcos我打算在另一個函數中使用它作爲參數 – manraj82 2011-03-09 13:12:25

+0

@ manraj82:檢查出 – Maxym 2011-03-09 13:47:28

+0

這仍然不能解決我得到的問題,我打算獲取多個postcode.basically的返回值我試圖創建一個通用的函數,我可以傳遞一個郵編作爲參數,並得到返回的長和緯度值。因爲這些值在回調中,我猜它不能被返回。 – manraj82 2011-03-09 15:31:57

相關問題