The problem is that you are measuring the straight line distance between the two coordinates and not accounting for the curvature of the earth. So while your distance computation is correct, it’s not what you want. What you want to compute is surface distance between the two points. I’m not sure we have a method in Cesium to do this yet (someone please correct me if I’m wrong). We know how, and we have code we can port to JavaScript, we just haven’t done it yet. I can look into adding it in the next few days if that’s okay with you.
This is still technically incorrect for large distances, but should get you much closer to what you want until we add actual surface distance calculation for you.
Hello,
as Matthew Amato noticed, your formula measures a straight line distance. To take the curvature of the Earth, I suggest you this algorithm (from cartographic points A and B, in radians):
var result;
//arc cos (sinLambdaA sinLambdaB + cos LambdaA cosLambdaB cosDeltaLongitude)*EarthRadius
result=Math.cos(A.getLongitude()-B.getLongitude());
result=result*Math.cos(A.getLatitude())*Math.cos(B.getLatitude());
result=result+Math.sin(A.getLatitude())*Math.sin(B.getLatitude());
result=Math.acos(result);
result=result*EarthRadius;
return result;
var distance = calculateDistance(new Cesium.Cartesian3.fromArray(clickPositions[0]), new Cesium.Cartesian3.fromArray(clickPositions[1]));
console.log("Distance: " + distance);
confirm("First point is: " + clickPositions[0] +
"\nSecond Point is: " + clickPositions[1] +
"\nDistance: " + distance);
} else {
confirm(“You have clicked on more than two points”);
}
Any help you can give would be greatly appreciated.
I agree, the calculateDistance() was not working correctly.
I did however find a solution by adapting a algorithm I found, which uses the cartographic positions instead and takes into account the height value for each point.
I was also going to ask, for the above area algorithm, if the positions array that is passed to Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray() is in Radians will the algorithm still work?
I ask because Radians seems to be the default unit for Cartesian3 instances in Cesium.
The stackoverflow solution calculates the distance over the earth’s curved surface, which is very different from the Cartesian3.distance() (straight line between two points).
Cartesian3 is always meters so that will not work with radians. In the algorithm the cartesianArrayToCartographicArray() is meant to convert X,Y,Z to latitude,longitude,height (in radians and meters).