我如何获得这些变量的值?

时间:2014-08-23 21:47:27

标签: javascript

我试图获取变量“纬度”和“经度”的值。

var latitude;
var longitude;
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("txtAddress").value;

geocoder.geocode({ 'address': address }, function (results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

    latitude = results[0].geometry.location.lat();
    longitude = results[0].geometry.location.lng();

    alert("Latitude: " + latitude + "\nLongitude: " + longitude);
  } else {
    alert("Request failed.")
  }
});

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为你不能。这是一个异步电话。

我处理类似事情的方式是在设置纬度和经度后立即调用你的下一个功能。

例如:

if (status == google.maps.GeocoderStatus.OK) {
      var latitude  = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();

      myNextFunction(latitude, longitude);
相关问题