【问题标题】:Distance calculation between two points using esri javascript api使用esri javascript api计算两点之间的距离
【发布时间】:2019-07-10 03:19:03
【问题描述】:

我们正在构建一个使用 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);
    }
  }

截图

选择两个点后,我注意到第二个点的纬度/经度值不正确。

这计算两点之间的距离为

实际距离应该如下(esri 小部件结果)

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

【问题讨论】:

  • 您是否检查了两个 json 对象console.log(firstPt, secondPt),因为它们在 distanceCalculator 函数中传递,以查看它们是否始终具有正确的坐标?
  • 是的,我在单步执行函数中的代码时验证了这一点。但是我注意到了一些事情,当我用第二个坐标纬度和经度创建点 B 时,它会重写为 -89.99 纬度和 53.973 长。这是不正确的。但是当我再次选择这些点时,它会显示正确的纬度和经度。
  • 这两个点存在于不同的层会有所不同吗?
  • @BelowtheRadar 谢谢大家的帮助,我知道我哪里出错了,我继续调试,发现纬度和经度设置不正确。它第二次起作用的原因是因为它是从数据库中提取数据而不是从图层中提取数据。我自己的脸蛋时刻。

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


【解决方案1】:

Egg on face 对我来说,我在导致此错误的代码中设置了纬度和经度错误的一点。谢谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2011-04-23
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多