繁体   English   中英

geocoder.geocode 响应延迟导致麻烦

[英]delay in response of geocoder.geocode causing trouble

我正在使用 geocoder.geocode 计算我在 mysql DB 中拥有的所有国家地址的纬度、经度。 我成功计算了 lat,long,甚至计算了从我的 lat,long 到所有地址 lat,long 的距离,并将这些距离存储在一个数组中。 但是当我访问数组时它是空的,并且 console.log 正在打印数组的所有内容。

   if (navigator.geolocation) {
       navigator.geolocation.getCurrentPosition(getLatLon, showError);
     }else{
     console.log("Geo Location is not supported");
   }

function getLatLon(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var country;
        geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(lat, lng);
  geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
        country = results[7].address_components[0].long_name;
        $.ajax({
          url:"getLocations.php",
          method:"POST",
          dataType:"JSON",
          data:{location:country},
          success: function(data){
            var i=0;
            var length = data.length;
            for(i;i<length;i++){
              if(isNaN(data[i])){
                var geocoder = new google.maps.Geocoder();
                var address = data[i];
                var distance = getDistance(address,latlng,data[i+1]);
                // myLocations.push(distance);
              }

            }
// This console.log prints all data
            console.log(myLocations);            
// But this piece of code is not working
            for(var iterations=0;iterations<myLocations.length;iterations++){
              console.log(myLocations[iterations]);
             }
          }
        });
      }
    }
  });
}

function getDistance(address,latlng,index){
  var distance;
geocoder.geocode( { 'address': address}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK)
                  {
                    var newdata = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
                    distance = (google.maps.geometry.spherical.computeDistanceBetween(newdata, latlng))*0.000621371;
                    var var1 = {distance: distance, index:index};
                    myLocations.push(var1);
                  }
                });
return distance;
}

  function showError(error){
      switch(error.code) {
        case error.PERMISSION_DENIED:
          var img = $('.arrow');
          img.css('display','none');
          console.log("User denied the request for Geolocation.");
      break;
    case error.POSITION_UNAVAILABLE:
          var img = $('.arrow');
          img.css('display','none');
      console.log("Location information is unavailable.");
      break;
    case error.TIMEOUT:
          var img = $('.arrow');
          img.css('display','none');
      console.log("The request to get user location timed out.");
      break;
    case error.UNKNOWN_ERROR:
          var img = $('.arrow');
          img.css('display','none');
      console.log("An unknown error occurred.");
      break;
  }
  }

可能对其他人有用,我使用了setTimeout(function(){ alert(myLocations); }, 3000); 页面加载后,它打印出我想要的整个数组。

暂无
暂无

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

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