Javascript嵌套函数-需要帮助才能返回结果
我正在尝试创建一个函数,从GoogleMapsJSAPI(v3)返回一个lat/long数组。我能够写出从API检索的数据,但无法将数据传递给变量。我想做这样的事情:Javascript嵌套函数-需要帮助才能返回结果,javascript,function,callback,Javascript,Function,Callback,我正在尝试创建一个函数,从GoogleMapsJSAPI(v3)返回一个lat/long数组。我能够写出从API检索的数据,但无法将数据传递给变量。我想做这样的事情: var latLong = getLatLong(Address); var geo = null; getLatLong('Dallas, TX', function(result) { // cache state geo = result; // do something with it showR
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
我创建的函数如下所示:
function getLatLong(loc, callback) {
var geocoder = new google.maps.Geocoder();
var array = new Array();
if (geocoder) {
geocoder.geocode({ 'address': loc }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;;
$.each(latLng, function(k, v) {
if (k == 'va' || k == 'wa') {
array.push(v);
}
});
var ret = array;
callback(ret);
} else {
console.log("Geocoding failed: " + status);
}
});
}
}
然后,我将函数赋值给一个变量,并调用它来获得lat/long
var geo = getLatLong('Dallas, TX', function(result) {
return result;
});
不幸的是,这会产生一个“未定义”的结果。我可以在函数中访问结果,但无法传递它。如何将函数的结果传递给要访问的变量
不幸的是,这会产生一个“未定义”的结果
你是说在“geo”变量中?如果是这样,那么您就错过了回调的要点。在getLatLong()调用返回后,将调用传入的函数。回调是异步函数,您需要使用回调来执行您选择的操作(例如更改状态、调整显示等)
试着把它改成
function(result) { alert('callback result: '+result); }
检查它是否被正确调用。如果您得到了正确的警报()
,则可能需要执行以下操作:
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
不幸的是,这会产生一个“未定义”的结果
你是说在“geo”变量中?如果是这样,那么您就错过了回调的要点。在getLatLong()调用返回后,将调用传入的函数。回调是异步函数,您需要使用回调来执行您选择的操作(例如更改状态、调整显示等)
试着把它改成
function(result) { alert('callback result: '+result); }
检查它是否被正确调用。如果您得到了正确的警报()
,则可能需要执行以下操作:
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
geo是未定义的,因为getLatLong()不返回任何内容(这意味着它返回未定义)。geo是未定义的,因为getLatLong()不返回任何内容(这意味着它返回未定义)。这里的关键点是
getLatLong
不返回值。这就是为什么geo
总是未定义的原因
要使其正常工作,您需要修改回调函数。大概是这样的:
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
这里的关键点是
getLatLong
不返回值。这就是为什么geo
总是未定义的原因
要使其正常工作,您需要修改回调函数。大概是这样的:
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
你不能按照你想要的方式来构造代码。问题在于
geocoder.geocode()
是一个异步操作。这就是它需要回调函数的原因。当您调用geocoder.geocode()
时,它会启动获取地理代码的请求,然后返回,让请求在后台运行。当请求最终完成时,google的api将执行您作为第二个参数传递给geocode()
的匿名函数
因此,当您调用getLatLong()
时,实际发生的顺序如下:
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
geocoder.geocode()
。(启动地理编码请求)getLatLong()
完成并返回未定义。(这是设置geo
的位置)数组中,然后调用您的回调()
那么,如果不能返回值,那么如何使用结果呢?像这样:
getLatLong('Dallas, TX', function(geo) {
// geo is passed to this function as a parameter
// move all code that requires `geo` into this function.
});
你不能按照你想要的方式来构造代码。问题在于
geocoder.geocode()
是一个异步操作。这就是它需要回调函数的原因。当您调用geocoder.geocode()
时,它会启动获取地理代码的请求,然后返回,让请求在后台运行。当请求最终完成时,google的api将执行您作为第二个参数传递给geocode()
的匿名函数
因此,当您调用getLatLong()
时,实际发生的顺序如下:
var latLong = getLatLong(Address);
var geo = null;
getLatLong('Dallas, TX', function(result) {
// cache state
geo = result;
// do something with it
showResultInSomeSpotOnThePage(result);
});
/* We can't use the "geo" variable contents immediately
* after getLatLong returns; it will be set at some time later.
* But there may be some use for keeping the result around indefinitely,
* we just have to be prepared for "geo" to be null if we check it
* before the callback is called.
*/
getLatLong('Dallas, TX', function(result)
{
var geo = result;
//continue to use geo here as you normally would
});
geocoder.geocode()
。(启动地理编码请求)getLatLong()
完成并返回未定义。(这是设置geo
的位置)数组中,然后调用您的回调()
那么,如果不能返回值,那么如何使用结果呢?像这样:
getLatLong('Dallas, TX', function(geo) {
// geo is passed to this function as a parameter
// move all code that requires `geo` into this function.
});
你是说你需要一个return somewere吗?你是说你需要一个return somewere吗?所以你可能想在getLatLong()中写“return callback(ret);”他不能
return callback(ret)
,因为ret
只有在谷歌的地理编码API调用回调后才能获得。是的,但核心问题是,geocoder.geocode()
的异步结果不能从OP的getLatLong()
函数同步返回。因此,您可能想在getLatLong()中编写“return callback(ret);”他不能返回回调(ret)
因为只有Google的geocoder API调用回调函数才能获得ret。是的,但核心问题是,geocoder.geocode()
的异步结果无法从OP的getLatLong()同步返回
function.getLatLong无法返回值,除非google的geocoder.geocode返回值,或者存在等待geocoder.geocode完成的阻塞(=错误!)调用。关键问题实际上是geocode调用的异步性,您的示例表明,geo
将在调用getLatLong()之后立即包含一个可用值