2011-03-09 17 views
15

Aşağıdaki uyarının altındaki boş bir uyarıyı kullandığımda? Neden?Google Haritalar JavaScript coğrafi kodlayıcısından bir boylam ve enlemi nasıl döndürebilirim?

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); 
    } 

cevap

22

sizin işlevi codeAddress yürütüldüğünde, çünkü gerçek nedeni loc boş dizi geocoder google ve boş loc döndürür için asenkron talebin yerine atama Google'dan gelen yanıt geldiğinde değer atanır. Başka bir deyişle, Allert tepki işleyicisi içinde olmalıdır:

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(); 
    } 

bu ... AJAX işliyor geri arama işleyicileri işlem sonuçlanır.


sen ve sen işleyicisi içinde gösterme fonksiyonunu yürütebileceği 'dispalying' coğrafi kodlama ayırmak istiyorsanız:

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()"> 


bunu daha genel yapabilir Eğer coğrafi kodu sadece görüntülemek için değil. Sonra codeAddress işleve parametre olarak geri arama tanımlayabiliriz: Cevabınız için

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

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

sayesinde, bir değişkene bu değerleri dönmek adına bir yolu var mı? bcos bir parametre – manraj82

+0

@ manraj82 olarak başka bir işlevde kullanmayı düşünmekteyim: – Maxym

+0

'u kontrol et, bu hala sorunu çözmüyor, mutiple postcode.basically için geri dönüş değerlerini almayı planlıyordum bir genel oluşturmaya çalışıyordum Bir posta kodunu bir argüman olarak iletebileceğim ve uzun ve lat değerlerini dönüşte alabildiğim fonksiyon. Bu değerler geri arandığında geri dönülemez sanırım. – manraj82

İlgili konular