Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - LatLng#distanceTo uses TurfMeasurement#distance
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Apr 1, 2019
1 parent 4adfc5d commit d348175
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import android.os.Parcelable;
import android.support.annotation.FloatRange;
import android.support.annotation.Keep;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.constants.GeometryConstants;
import com.mapbox.turf.TurfMeasurement;

import static com.mapbox.turf.TurfConstants.UNIT_METRES;


/**
Expand Down Expand Up @@ -209,7 +213,8 @@ public double getAltitude() {
@NonNull
public LatLng wrap() {
return new LatLng(latitude, wrap(longitude,
GeometryConstants.MIN_WRAP_LONGITUDE, GeometryConstants.MAX_WRAP_LONGITUDE));
GeometryConstants.MIN_WRAP_LONGITUDE, GeometryConstants.MAX_WRAP_LONGITUDE)
);
}


Expand All @@ -218,8 +223,10 @@ public LatLng wrap() {
* <p>
* Same formula as used in Core GL (wrap.hpp)
* std::fmod((std::fmod((value - min), d) + d), d) + min;
*
* </p>
* <p>
* Multiples of max value will be wrapped to max.
* </p>
*
* @param value Value to wrap
* @param min Minimum value
Expand Down Expand Up @@ -318,24 +325,10 @@ public void writeToParcel(@NonNull Parcel out, int flags) {
* @return distance in meters
*/
public double distanceTo(@NonNull LatLng other) {
if (latitude == other.latitude && longitude == other.longitude) {
// return 0.0 to avoid a NaN
return 0.0;
}

final double a1 = Math.toRadians(this.latitude);
final double a2 = Math.toRadians(this.longitude);
final double b1 = Math.toRadians(other.getLatitude());
final double b2 = Math.toRadians(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 GeometryConstants.RADIUS_EARTH_METERS * tt;
return TurfMeasurement.distance(
Point.fromLngLat(longitude, latitude),
Point.fromLngLat(other.getLongitude(), other.getLatitude()),
UNIT_METRES
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void testDistanceTo() {
LatLng latLng2 = new LatLng(1.0, 1.0);
assertEquals("distances should match",
latLng1.distanceTo(latLng2),
157425.53710839353, DELTA);
157298.7453847275, DELTA);
}

@Test
Expand All @@ -186,6 +186,15 @@ public void testDistanceToSamePoint() {
assertEquals("distance should match", 0.0, distance, DELTA);
}

// Regression test for #14216
@Test
public void testDistanceToClosePointNotNaN() {
LatLng latLng = new LatLng(40.00599, -105.29261);
LatLng other = new LatLng(40.005990000000025, -105.29260999999997);
double distance = latLng.distanceTo(other);
assertNotEquals(distance, Double.NaN);
}

@Test
public void testLocationProvider() {
double latitude = 1.2;
Expand Down

0 comments on commit d348175

Please sign in to comment.