2013-10-11 90 views
0

對不起,我知道這已被問了上千次,但我已經閱讀了回覆,但我仍然沒有得到它。我是Javascript新手(實際上我是從昨天開始的),我有以下問題:未定義的返回值。在這種情況下如何使用回調?

我有一個異步函數,我需要返回的值,但它當然是未定義的。我已閱讀了回調,但我不確定它們是如何工作的。

的功能如下:

function getLatLong(address){ 
     var geo = new google.maps.Geocoder; 

     geo.geocode({'address':address},function(results, status){ 
       if (status == google.maps.GeocoderStatus.OK) { 

       var returnedLatLng = []; 
       returnedLatLng["lat"] = results[0].geometry.location.lat(); 
       returnedLatLng["lgn"] = results[0].geometry.location.lng(); 
       locationTarget = new google.maps.LatLng(returnedLatLng.lat,returnedLatLng.lgn); 
       alert(locationTarget); 
       return locationTarget; 
       } else { 
       alert("Geocode was not successful for the following reason: " + status); 
       } 

     }); 
    } 

我從初始化調用這個函數()函數,我做這種方式:

var location = getLatLong(address); 

好吧,我的問題是回調如何幫助我在這裏?如果可能的話,我應該使用什麼代碼?

非常感謝! (這是我第一次真正的第一個問題在這裏!)

回答

0

最基本的解決方案開始將在全球範圍內確定自己的位置並給你已經有回調回應:

var location; 
function getLatLong(address){ 
     var geo = new google.maps.Geocoder; 

     geo.geocode({'address':address},function(results, status){ 
       if (status == google.maps.GeocoderStatus.OK) { 

       var returnedLatLng = []; 
       returnedLatLng["lat"] = results[0].geometry.location.lat(); 
       returnedLatLng["lgn"] = results[0].geometry.location.lng(); 
       locationTarget = new google.maps.LatLng(returnedLatLng.lat,returnedLatLng.lgn); 
       alert(locationTarget); 
       location = locationTarget; 
       // Additional logic you are using the location for 
       // After this point your location is defined. 
       } else { 
       alert("Geocode was not successful for the following reason: " + status); 
       } 

     }); 
    } 
getLatLong(address) 

所以基本上你的邏輯需要不是基於調用此方法返回位置,而是調用geocode函數回調後發生的事件。

+0

謝謝你這樣我快速的解答。我試圖做那樣的事情。我完全忘記提及這一點。無論如何,它沒有奏效。一旦我離開函數,全局變量就不再被定義。 你說「並回應你已經有的回調:」我很抱歉,但我已經有一個回調?我似乎並不瞭解這個概念 – gpanich

+0

@gpanich被定義爲地理編碼函數參數的函數是一個回調函數。在調用回調之前,全局變量將是未定義的。 –

0

試試這個:

var locationCallback = function(location){ 
     // your fancy code here 
    }; 
function getLatLong(address){ 
     var geo = new google.maps.Geocoder; 

     geo.geocode({'address':address},function(results, status){ 
       if (status == google.maps.GeocoderStatus.OK) { 

       var returnedLatLng = []; 
       returnedLatLng["lat"] = results[0].geometry.location.lat(); 
       returnedLatLng["lgn"] = results[0].geometry.location.lng(); 
       locationTarget = new google.maps.LatLng(returnedLatLng.lat,returnedLatLng.lgn); 
       alert(locationTarget); 

       //this is your callback 
       locationCallback(locationTarget); 


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

     }); 
    } 
getLatLong(address) 
+0

謝謝你的回答!但是我有很多問題,現在我覺得很愚蠢,但我完全不瞭解這個。「你在這裏的代碼是什麼」是什麼意思?你在說什麼代碼?現在是否將「返回值」存儲在locationCallback中?最後..爲什麼在那裏調用函數(getLatLong(address),我的意思是)。 – gpanich

+0

我假設你想對locationTarget的內容做些什麼。所以當調用回調函數時,你可以確定你的locationTarget在那裏,你可以處理結果。 – webduvet

相關問題