开发者

How do I return a variable from Google Maps JavaScript geocoder callback?

开发者 https://www.devze.com 2023-01-02 10:07 出处:网络
I am working with the google maps API and whenever I return the variable to the initialize function from the codeLatLng function it claims undefined. If I print the variable from the codeLatLng it sho

I am working with the google maps API and whenever I return the variable to the initialize function from the codeLatLng function it claims undefined. If I print the variable from the codeLatLng it shows up fine.

  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);
        }
      });
    }
  }

prints out undefined

If I do:

  var geocoder;
  function initialize() {
    geocoder = n开发者_开发技巧ew 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);
        }
      });
    }
  }

prints out New York, NY 10012, USA


You can't return the value from the function, the value doesn't exist yet when the function returns.

The geocode method makes an asynchonous call and uses a callback to handle the result, so you have to do the same in the codeLatLng function:

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);
      }
    });
  }
}


You're making an asynchronous request, your codeLatLng() function has finished and returned long before the geocoder is done.

If you need the geocoder data to continue, you'll have to chain your functions together:

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);
}


You can get value using localstorage.

 geocoder.geocode({
            'address': address,             
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
            }                             
           localStorage.setItem("endlat", latitude);
           localStorage.setItem("endlng", longitude);
            });
var end_lat = localStorage.getItem("endlat");
var end_lng = localStorage.getItem("endlng");

But it returns previous value.. Returns current value when we click twice...


pass geocoder as a parameter to the codeLatLng() function.

function codeLatLng(geocoder) {

call it like so in your initialize function:

var addr = codeLatLng(geocoder);


That return is not returning from codeLatLng; it's returning from the anonymous function being passed to geocoder.geocode.

I think you'll need to pass the data using another mechanism e.g. a global variable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号