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

[android] union should take date line into consideration #11308

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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