2013-08-15 9 views
7

Proszę, pomóżcie mi z google geocoder.geocode. Wiem, że ta funkcja działa asynchronicznie, ale nie wiem jak sobie z nią poradzić.Jak czekać na google geocoder.geocode?

Jak czekać na wynik? Oto mój kod: Mój kod nie czekają na geocode.geocoder, więc zamiast geolokacji otrzymuję undefined.

<!DOCTYPE html> 
<html>  
    <head>   
    <title>Geocoding service  
    </title>   
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
    <meta http-equiv="content-language" content="cs" />   

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">    
    <link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet">  
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>  
<script> 

function codeAddress(callback) { 

    var address = document.getElementById('address').value; 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    },callback); 
} 


function geocoder(result) { 
alert(codeAddress()); 
} 

function button() { 
    geocoder(codeAddress) ; 
    } 



    </script>  
    </head>  
    <body>   
    <div id="panel">    
     <input id="address" type="textbox" value="address">    
     <input type="button" value="Go" onclick="button()">   
    </div>   
    <div id="map-canvas">  
    </div>  
    </body> 
</html> 

Odpowiedz

0

znalazłem rozwiązanie podziękować strony http://recurial.com/programming/understanding-callback-functions-in-javascript/ gdzie jest zwrotna krok po kroku wyjaśnione. Kod

Tu pracuje:

<!DOCTYPE html> 
<html>  
    <head>   
    <title>Geocoding service  
    </title>   
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
    <meta http-equiv="content-language" content="cs" />   

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">    
    <link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet">  
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>  
<script> 

function codeAddress(callback) { 

    var address = document.getElementById('address').value; 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    souradnice = [results[0].geometry.location.lat(),results[0].geometry.location.lng()]; 
    callback(souradnice); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 


function gogo(){ 
codeAddress(function(num) { 
    alert(num); 

}); 
} 






    </script>  
    </head>  
    <body>   
    <div id="panel">    
     <input id="address" type="textbox" value="address">    
     <input type="button" value="Go" onclick="gogo()">   
    </div>   
    <div id="map-canvas">  
    </div>  
    </body> 
</html> 
+0

co jest num tutaj w parametrze funkcji – Techy

Powiązane problemy