简体   繁体   English

异步Google Maps请求抛出Javascript

[英]Asynchronous Google Maps request throwing off Javascript

I'm working on someone else's code, and I am trying to use Google maps API to convert a street address into latitude and longitude coordinates: var start_lng; 我正在处理其他人的代码,并且尝试使用Google Maps API将街道地址转换为纬度和经度坐标:var start_lng; var end_lat; var end_lat; var end_lng; var end_lng; getLat(start); getLat(start); getLng(start); getLng(开始);

  function getLat(loc) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': loc}, function postcodesearch(results, status) 
    {   
      if (status == google.maps.GeocoderStatus.OK) 
    {
      var lat = results[0].geometry.location.lat();
      alert(lat);
      return lat;

      //alert(start_lng);
    }
  else {
    alert("Error");
  }
  });
 }
function getLng(loc) {
var geocoder_lng = new google.maps.Geocoder();
  geocoder_lng.geocode({'address': loc}, function postcodesearch(results, status) 
{   
  if (status == google.maps.GeocoderStatus.OK) 
{
  var lng = results[0].geometry.location.lng();
  alert(lng);
  return lng;

}
  else {
 alert("Error");
   }
    });
    }

So far, so good. 到现在为止还挺好。 In the code below, I take that latitude and longitude and perform various calculations with them: 在下面的代码中,我采用该经度和纬度并对它们执行各种计算:

  alert("DO REST");
  var start_p = new google.maps.LatLng(start_lat, start_lng);
  alert(start_p);
  var end_p = new google.maps.LatLng(end_lat, end_lng);

The problem is that since the call that gets the latitude and longitude is asynchronous, the code below doesn't wait to get the values before it tries to use them. 问题在于,由于获取纬度和经度的调用是异步的,因此下面的代码在尝试使用它们之前不会等待获取这些值。 They show up as undefined. 它们显示为未定义。 I tried putting the second half of the code in its own function and calling that at the end of the longitude request, but that only worked if the longitude call happened to finish first. 我尝试将代码的后半部分放在自己的函数中,并在经度请求的末尾调用它,但这仅在经度调用恰好首先完成时才有效。 I also tried $.ajax({ async:false}) but that didn't do anything. 我也尝试了$ .ajax({async:false}),但是没有做任何事情。 This is my first time dealing with AJAX, so I'm really not sure which way to go. 这是我第一次处理AJAX,所以我真的不确定该走哪条路。

For the "handler" code, use a callback instead: 对于“处理程序”代码,请改用回调:

function getLatLng(loc, callback) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': loc}, function postcodesearch(results, status) 
    {   
        if (status == google.maps.GeocoderStatus.OK) 
        {
            var lat = results[0].geometry.location.lat();
            var lng = results[0].geometry.location.lng();
            callback(lat, lng);
        }
    }
}

getLatLng(function(lat, lng) {
    alert("DO REST");
    var start_p = new google.maps.LatLng(lat, lng);
});

If you need to make two calls to the geocoder, then you can always nest those calls, and put the callback after both are complete. 如果您需要对地址解析器进行两次调用,那么您始终可以嵌套这些调用,并在两者均完成后放置回调。 Something like: 就像是:

function getLatLng(locStart, locEnd, callback) {
    geocoder.geocode({ }, function(results1) {

        geocoder.geocode({ }, function(results2) {

            callback(results1.lat, results1.lng, results2.lat, results2.lng);
        });
    });
}

getLatLng(loc1, loc2, function(lat1, lng1, lat2, lng2) {

});

use global flag variables 使用全局标志变量

for example: 例如:

var flag_lat = var flag_lnt = 0;

then add in your postcodesearch functions flag_lat=1; 然后添加您的postcodesearch功能flag_lat=1; and flag_lng=1; 并且flag_lng=1; respectively 分别

and in the end of each postcodesearch function check 并在每个postcodesearch函数末尾进行检查

if (flag_lat && flag_lng) 
{ 
  do_the_things_function(); 
  flag_lat = flag_lng = 0; //reset the flags
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM