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

我正在使用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


Smart猫小萌
浏览 472回答 3
3回答

慕村9548890

您无法从函数返回值,该函数返回时该值尚不存在。该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);      }    });  }}

智慧大石

您正在发起一个异步请求,您的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);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript