Distance calculation between two points using esri javascript api

1k views Asked by At

We are building a web application that uses an esri map. What i am trying to do is calculate the distance between two points. The coordinates for these points are in two separate json objects. Next i generate two esri points by parsing the latitude and the longitude values from these two json objects. Esri points require spatial reference, so i set the spatial reference of the mapView as the spatial reference for the two points.

The Problem: When i select the two points, it completely rewrites the latitude and the longitude of one of the points. This results in a ridiculous calculation of the two points.

This is what i have tried

//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);
    }
  }

Screenshots

After selecting the two points , i noticed that the second point has incorrect lat/long values. enter image description here

This calculates the distance between the two points as enter image description here

the actual distance is supposed to be the following (esri widget result)

enter image description here

If i were to select the two points again, it generates the correct distance

enter image description here

1

There are 1 answers

0
goodcat On

Egg on face moment for me, I was setting the lat and long wrong one point in the code that resulted in this error. Thank you all for the help.