With certain LatLng values (e.g. LatLng(40.71199035644531, -74.0081)) we are getting a NaN return value when finding the distance to the same point.
LatLng ll1 = new LatLng(40.71199035644531, -74.0081);
LatLng ll2 = new LatLng(40.71199035644531, -74.0081);
double distance = ll1.distanceTo(ll2); // distance: NaN
It appears that in this particular case the sum of t1, t2 & t3 equals 1.0000000000000002 instead of 1 which then causes Math.acos() to return NaN since its > 1.
public double distanceTo(LatLng other) {
final double a1 = MathConstants.DEG2RAD * this.latitude;
final double a2 = MathConstants.DEG2RAD * this.longitude;
final double b1 = MathConstants.DEG2RAD * other.getLatitude();
final double b2 = MathConstants.DEG2RAD * other.getLongitude();
final double cosa1 = Math.cos(a1);
final double cosb1 = Math.cos(b1);
final double t1 = cosa1 * Math.cos(a2) * cosb1 * Math.cos(b2);
final double t2 = cosa1 * Math.sin(a2) * cosb1 * Math.sin(b2);
final double t3 = Math.sin(a1) * Math.sin(b1);
final double tt = Math.acos(t1 + t2 + t3);
return GeoConstants.RADIUS_EARTH_METERS * tt;
}
With certain LatLng values (e.g. LatLng(40.71199035644531, -74.0081)) we are getting a NaN return value when finding the distance to the same point.
It appears that in this particular case the sum of t1, t2 & t3 equals 1.0000000000000002 instead of 1 which then causes
Math.acos()
to return NaN since its > 1.