2013-10-11 41 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

相关问题