1

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

Below the Radar
  • 7,321
  • 11
  • 63
  • 142
goodcat
  • 200
  • 2
  • 9
  • 1
    Have you inspected the two json objects `console.log(firstPt, secondPt)` as they are passed in the distanceCalculator function to see if they always have the correct coordinates? – Below the Radar Feb 18 '19 at 19:00
  • yes i verified this as i stepped through the code in the function. However i noticed something, when i create pointB with second coordinates latitude and longitude, it rewrites to -89.99 lat and 53.973 long. this is incorrect. But when i select the points again, it shows the correct lat and long. – goodcat Feb 18 '19 at 19:04
  • Does the fact that these two points exist on different layers make a difference? – goodcat Feb 18 '19 at 19:05
  • 1
    @BelowtheRadar Thank you for all the help, i figured out where im going wrong, i kept debugging further down and i found out that the lat and long was being incorrectly set. The reason why it worked the second time was because it was pulling the data from the database and not from the layers.Egg on face moment for myself. – goodcat Feb 19 '19 at 17:17

1 Answers1

1

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.

goodcat
  • 200
  • 2
  • 9