为了账号安全,请及时绑定邮箱和手机立即绑定

如何从Google Maps JavaScript geocoder回调返回变量?

如何从Google Maps JavaScript geocoder回调返回变量?

Smart猫小萌 2019-10-09 17:36:35
我正在使用Google Maps API,并且每当我从codeLatLng函数将变量返回到initialize函数时,它都声明未定义。如果我从codeLatLng打印变量,它将显示正常。  var geocoder;  function initialize() {    geocoder = new google.maps.Geocoder();    var latlng = new google.maps.LatLng(40.730885,-73.997383);    var addr = codeLatLng();    document.write(addr);  }  function codeLatLng() {    var latlng = new google.maps.LatLng(40.730885,-73.997383);    if (geocoder) {      geocoder.geocode({'latLng': latlng}, function(results, status) {        if (status == google.maps.GeocoderStatus.OK) {          if (results[1]) {                return results[1].formatted_address;          } else {            alert("No results found");          }        } else {          alert("Geocoder failed due to: " + status);        }      });    }  }打印出未定义如果我做:  var geocoder;  function initialize() {    geocoder = new google.maps.Geocoder();    var latlng = new google.maps.LatLng(40.730885,-73.997383);    codeLatLng();  }  function codeLatLng() {    var latlng = new google.maps.LatLng(40.730885,-73.997383);    if (geocoder) {      geocoder.geocode({'latLng': latlng}, function(results, status) {        if (status == google.maps.GeocoderStatus.OK) {          if (results[1]) {                document.write(results[1].formatted_address);          } else {            alert("No results found");          }        } else {          alert("Geocoder failed due to: " + status);        }      });    }  }打印出美国纽约州纽约10012
查看完整描述

3 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

您无法从函数返回值,该函数返回时该值尚不存在。


该geocode方法进行异步调用并使用回调来处理结果,因此您必须在codeLatLng函数中执行相同的操作:


var geocoder;

function initialize() {

  geocoder = new google.maps.Geocoder();

  var latlng = new google.maps.LatLng(40.730885,-73.997383);

  codeLatLng(function(addr){

    alert(addr);

  });

}


function codeLatLng(callback) {

  var latlng = new google.maps.LatLng(40.730885,-73.997383);

  if (geocoder) {

    geocoder.geocode({'latLng': latlng}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {

        if (results[1]) {

          callback(results[1].formatted_address);

        } else {

          alert("No results found");

        }

      } else {

        alert("Geocoder failed due to: " + status);

      }

    });

  }

}


查看完整回答
反对 回复 2019-10-09
?
智慧大石

TA贡献1946条经验 获得超3个赞

您正在发起一个异步请求,您的codeLatLng()函数已经完成并且在地理编码器完成之前返回了很长时间。


如果您需要继续使用地址解析器数据,则必须将功能链接在一起:


function initialize() {

    geocoder = new google.maps.Geocoder();

    codeLatLng();

}

function codeLatLng() {

    var latlng = new google.maps.LatLng(40.730885,-73.997383);

    if (geocoder) {

        geocoder.geocode({'latLng': latlng}, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {

                    if (results[1]) {

                        initContinued(results[1].formatted_address);

                    } else {

                        alert("No results found");

                    }

                } else {

                    alert("Geocoder failed due to: " + status);

                }

        });

      }


}

function initContinued(addr) {

    alert(addr);

}


查看完整回答
反对 回复 2019-10-09
  • 3 回答
  • 0 关注
  • 471 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号