使用esri JavaScript API计算两点之间的距离

时间:2019-02-15 23:51:59

标签: angular7 esri arcgis-js-api esri-javascript-api

我们正在构建一个使用esri映射的Web应用程序。我想做的是计算两点之间的距离。这些点的坐标在两个单独的json对象中。 接下来,我通过解析来自这两个json对象的纬度和经度值来生成两个esri点。 Esri点需要空间参考,因此我将mapView的空间参考设置为两个点的空间参考。

问题:当我选择两个点时,它会完全重写其中一个点的纬度和经度。这导致对这两点的荒谬计算。

这是我尝试过的

//calling the distance function

distance = this.distanceCalculator(incidentCenter, residence);

//distance calc function
  private distanceCalculator(firstPt, secondPt): any {
    //this.measureLayer.removeAll();

    // When calculating the distance betweeen two points , we need to decypher the points coming in and attach a spatial reference.
    const pointA: esri.Point = new this.Point({
      spatialReference: this.mapView$.value.spatialReference,
      latitude: firstPt.latitude,
      longitude: firstPt.longitude
    });

    //so whats happening here is that 
    const pointB: esri.Point = new this.Point({
      spatialReference: this.mapView$.value.spatialReference,
      latitude: secondPt.latitude,
      longitude: secondPt.longitude
    });


    console.log("Point a and B");
    console.log("point a: ", pointA);
    console.log("point b: ", pointB);

    // Next we call the GemoetryEngine distance function and calculate the distance between the two points.

    try {
      const miles = this.GeometryEngine.distance(pointA, pointB, 'kilometers');
      const kms = miles * this.MilesToKm;
      return kms.toFixed(2);
    } catch (e) {
      this.logger.error('Error indistanceCalculator ', e);
    }
  }

屏幕截图

在选择了两个点之后,我注意到第二个点的经纬度值不正确。 enter image description here

这将两点之间的距离计算为 enter image description here

实际距离应为以下(esri小部件结果)

enter image description here

如果我再次选择两个点,它将生成正确的距离

enter image description here

1 个答案:

答案 0 :(得分:1)

对我来说,我在代码中设置了经纬度长期错误的一点,从而导致此错误。谢谢大家的帮助。