在Google Geocode中使用回调函数 [英] Using a callback function with Google Geocode

查看:300
本文介绍了在Google Geocode中使用回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在一直在为此苦苦挣扎几个小时,甚至在阅读了关于Stack的几个例子之后,我一直无法得到这个工作。这不会帮助我是JS新手。



我试图从Google Geocoder API中检索有关地址的信息,然后将对象到另一个功能。根据我的阅读,我明白我用来检索信息的函数是异步的,因此我需要使用回调函数来读取它。但是,当我尝试这样做时,我的控制台仍然返回'undefined'。我知道这些信息来自Google,因为当我在结果对象上使用console.log()时,它会正确返回。



反正,这就是我使用:

  function onSuccess(position){
getLocationData(position,function(locationData){
console.log(locationData);
});
}

函数getLocationData(position,callback){
geocoder = new google.maps.Geocoder();
var location ='Billings,MT';

if(geocoder){
geocoder.geocode({'address':location},function(results,status){
if(status == google.maps.GeocoderStatus .OK){
返回结果[0];
}
});
}
callback();
}

就像我刚才提到的那样,我只用'undefined'。如果我把'console.log(results [0])'放在getLocationData()返回之上,返回的对象是正确的。任何帮助将不胜感激。

解决方案

您的问题是,您没有将回调连接到回报。由于 geocode()函数本身已经是异步的,因此 return 在这里没有任何影响。相反,您必须将您在此返回的值直接传递给回调函数。像这样:



function getLocationData(position,callback){
geocoder = new google.maps.Geocoder();
var location ='Billings,MT';

if(geocoder){
geocoder.geocode({'address':location},function(results,status){
if(status == google.maps.GeocoderStatus .OK){
callback(results [0]);
}
});
}
}


I've been struggling with this for a few hours now, and even after reading several examples on Stack I've been unable to get this working. It doesn't help that I'm a JS newbie.

I'm trying to retrieve information about an address from the Google Geocoder API, and then pass the object over to another function. Per my reading I understand that function I'm using to retrieve the information is asynchronous, and therefore I need to use a callback function to read it. However, when I attempt to do this I still get 'undefined' returned by my console. I know that the information is coming from Google fine, since when I use console.log() on the result object it returns correctly.

Anyways, here's what I'm working with:

function onSuccess(position) {
  getLocationData(position, function(locationData) {
    console.log(locationData);
  });   
}

function getLocationData(position, callback) {
  geocoder = new google.maps.Geocoder();
  var location = 'Billings,MT';

  if( geocoder ) {
    geocoder.geocode({ 'address': location }, function (results, status) {
      if( status == google.maps.GeocoderStatus.OK ) {
        return results[0];
      }
    });
  }
  callback();
}

Like I mentioned though, all I get with this is 'undefined'. If I put 'console.log(results[0])' above the getLocationData() return, the object returned is correct though. Any help would be much appreciated.

解决方案

Your problem is, that you didn't connect the callback to the return. As the geocode() function itself is already asynchronous, the return doesn't have any effect there. Instead you have to pass the values you are returning here directly to the callback-function. Like this:

function getLocationData(position, callback) {
  geocoder = new google.maps.Geocoder();
  var location = 'Billings,MT';

  if( geocoder ) {
    geocoder.geocode({ 'address': location }, function (results, status) {
      if( status == google.maps.GeocoderStatus.OK ) {
        callback(results[0]);
      }
    });
  }
}

这篇关于在Google Geocode中使用回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆