类内部的离子基本功能未完全执行

时间:2019-03-12 05:44:14

标签: asynchronous ionic-framework ionic3

  calculate_distance(lat1: number, lat2: number, long1: number, long2: number) {
    console.log("Inside getting calculate_distance");
    console.log(lat1);
    console.log(lat2);
    console.log(long1);
    console.log(long2);
    let p = 0.017453292519943295;    // Math.PI / 180
    let c = Math.cos;
    let a = 0.5 - c((lat1 - lat2) * p) / 2 + c(lat2 * p) * c((lat1) * p) * (1 - c(((long1 - long2) * p))) / 2;
    let dis = (12742 * Math.asin(Math.sqrt(a))); // 2 * R; R = 6371 km
    return dis.toFixed(2);
  }

在其他函数中调用此函数,但我没有得到距离值。

1 个答案:

答案 0 :(得分:0)

我刚刚在我的函数中添加了一个promise返回类型。这样它将返回我的值。

var _URL = window.URL || window.webkitURL;

jQuery("#filecheck").change(function() {
  var file, img;

  if ((file = this.files[0])) {
    img = new Image();

    img.onload = function() {
      if (this.width < 1800 && this.height < 1800) {
        jQuery('.pass').css('display', 'none');
        jQuery('.fail').css('display', 'block');
      }

      else {
        jQuery('.pass').css('display', 'block');
        jQuery('.fail').css('display', 'none');
      }
    };
    img.src = _URL.createObjectURL(file);
  }
});

并这样称呼calculateDistance

calculateDistance(lat1: number, lat2: number, long1: number, long2: number):Promise<any> {
    console.log("Inside getting calculate_distance");
    console.log(lat1);
    console.log(lat2);
    console.log(long1);
    console.log(long2);
    let p = 0.017453292519943295;    // Math.PI / 180
    let c = Math.cos;
    let a = 0.5 - c((lat1 - lat2) * p) / 2 + c(lat2 * p) * c((lat1) * p) * (1 - c(((long1 - long2) * p))) / 2;
    let dis = (12742 * Math.asin(Math.sqrt(a))); // 2 * R; R = 6371 km
    return dis.toFixed(2);
  }