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

Commit

Permalink
[android] LatLngBounds.union() should take date line into account
Browse files Browse the repository at this point in the history
  • Loading branch information
osana committed Feb 28, 2018
1 parent ab27e24 commit f4833a2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,27 @@ public void outerUnion() {
.build());
}

@Test
public void unionOverDateLine() {
LatLngBounds latLngBounds1 = new LatLngBounds.Builder()
.include(new LatLng(10, 170))
.include(new LatLng(0, 160))
.build();

LatLngBounds latLngBounds2 = new LatLngBounds.Builder()
.include(new LatLng(0, -170))
.include(new LatLng(-10, -160))
.build();

assertEquals("outer union should match",
latLngBounds1.union(latLngBounds2),
new LatLngBounds.Builder()
.include(new LatLng(10, 160))
.include(new LatLng(-10, -160))
.build());
}


@Test
public void northWest() {
double minLat = 5;
Expand Down

0 comments on commit f4833a2

Please sign in to comment.