From bbd18d39d56f4a8ec5ea1f7a317066ca6f6813ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cosana=E2=80=9D?= Date: Mon, 26 Feb 2018 02:42:03 +0100 Subject: [PATCH] [android] union should take date line into consideration --- .../mapboxsdk/geometry/LatLngBounds.java | 19 +++++++++++++++---- .../mapbox/mapboxsdk/geometry/LatLngSpan.java | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java index fc8d2ec8f05..07c6c5462fd 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngBounds.java @@ -438,10 +438,21 @@ public LatLngBounds union(LatLngBounds bounds) { * @return BoundingBox */ public LatLngBounds union(final double latNorth, final double lonEast, final double latSouth, final double lonWest) { - return new LatLngBounds((this.latitudeNorth < latNorth) ? latNorth : this.latitudeNorth, - (this.longitudeEast < lonEast) ? lonEast : this.longitudeEast, - (this.latitudeSouth > latSouth) ? latSouth : this.latitudeSouth, - (this.longitudeWest > lonWest) ? lonWest : this.longitudeWest); + double north = (this.latitudeNorth < latNorth) ? latNorth : this.latitudeNorth; + double south = (this.latitudeSouth > latSouth) ? latSouth : this.latitudeSouth; + + if (LatLngSpan.getLongitudeSpan(lonEast, this.longitudeWest) < + LatLngSpan.getLongitudeSpan(this.longitudeEast, lonWest)) { + return new LatLngBounds(north, + lonEast, + south, + this.longitudeWest); + } + + return new LatLngBounds(north, + this.longitudeEast, + south, + lonWest); } /** diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngSpan.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngSpan.java index 322c7dfb74a..133949f743b 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngSpan.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/geometry/LatLngSpan.java @@ -4,6 +4,8 @@ import android.os.Parcelable; import android.support.annotation.NonNull; +import com.mapbox.mapboxsdk.constants.GeometryConstants; + /** * A geographical span defined by its latitude and longitude span. */ @@ -136,4 +138,20 @@ public int hashCode() { result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } + + /** + * Get the absolute distance, in degrees, between the west and + * east boundaries of this LatLngBounds + * + * @return Span distance + */ + static double getLongitudeSpan(double east, double west) { + double longSpan = Math.abs(east - west); + if (east > west) { + return longSpan; + } + + // shortest span contains antimeridian + return GeometryConstants.LONGITUDE_SPAN - longSpan; + } }