Geocoder.geocode的响应延迟导致问题

时间:2020-06-04 07:13:42

标签: javascript html ajax

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

1 个答案:

答案 0 :(得分:0)

也许对其他人有用,我在页面加载后使用了val columns = List("PK","Col1","Col3","A","ColD","C","ColC") val newDF = spark.read.csv("File1","File2","File3").select(columns.map(col):_*) newDf.write.parquet("/parquet.file") ,并根据需要打印了整个数组。