等待谷歌地图地理编码器? [英] Waiting for google maps geocoder?

查看:86
本文介绍了等待谷歌地图地理编码器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  geo = function(options){
geocoder.geocode(options,function(results,status){
if(status == google.maps.GeocoderStatus .OK){
var x = results;
alert('pear');
return x;
} else {
return -1;
}
});

$ b getAddr = function(addr){
if(typeof addr!='undefined'&& addr!= null){
var blah = geo({address:addr,});
alert('apple');
return blah;
}
返回-1;





$ b因此,当我调用getAddr时,我得到的是未定义的,梨。我意识到谷歌地图异步地理编码,但有没有办法让这项工作? 解决方案

你将无法做到就这样。你有一个异步调用谷歌的地理编码器,这意味着你将无法使getAddr返回结果。相反,你应该这样做:

  getAddr = function(addr,f){
if(typeof addr! ='undefined'&& addr!= null){
geocoder.geocode({address:addr,},function(results,status){
if(status == google.maps.GeocoderStatus .OK){
f(results);
}
});
}
返回-1;
}

然后在代码中使用这样的代码:

  getAddr(addr,function(res){
// blah blah,无论您使用
做什么// //返回的是什么从getAddr以前的
//你只需要用res而不是
//例如:
alert(res);
});

编辑:如果你想要你也可以添加更多的状态验证:

  getAddr = function(addr,f){
if(typeof addr!='undefined'&& addr!= null){
geocoder.geocode({address:addr,},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
f('ok',results) ;
} else {
f('error',null);
}
});
} else {
f('error',null);


你可以像这样使用它:

  getAddr(addr,function(status,res){
// blah blah,无论您使用
做什么//之前从getAddr返回的内容
//您只需使用res而不是
//例如:
if(status =='ok'){
alert(res) ;
} else {
alert(Error)
}
});


geo = function(options){
    geocoder.geocode( options, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var x = results;
            alert('pear');
            return x;
        } else {
            return -1;
            }
        });
    }

getAddr = function(addr){
    if(typeof addr != 'undefined' && addr != null) {
        var blah = geo({ address: addr, });
                    alert('apple');
                    return blah;
    }
    return -1;
}

So when I call getAddr I get undefined, also apple is alerted first and then pear. I realize that google maps geocodes asynchronously, but is there a way to make this work?

解决方案

You will not be able to do it that way. You have an asynchronous call to google's geocoder, which means you will not be able to have the getAddr return the results. Instead you should do something like this:

getAddr = function(addr, f){
    if(typeof addr != 'undefined' && addr != null) {
        geocoder.geocode( { address: addr, }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            f(results);
          }
        });
    }
    return -1;
}

And then you use in your code like that:

getAddr(addr, function(res) {
  // blah blah, whatever you would do with 
  // what was returned from getAddr previously
  // you just use res instead
  // For example:
  alert(res);
});

EDIT: If you want to you could also add more status validation:

getAddr = function(addr, f){
    if(typeof addr != 'undefined' && addr != null) {
        geocoder.geocode( { address: addr, }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            f('ok', results);
          } else {
            f('error', null);
          }
        });
    } else {
      f('error', null);
    }
}

And you can use it like that:

getAddr(addr, function(status, res) {
  // blah blah, whatever you would do with 
  // what was returned from getAddr previously
  // you just use res instead
  // For example:
  if (status == 'ok') {
    alert(res);
  } else {
    alert("Error")
  }
});

这篇关于等待谷歌地图地理编码器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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