[android] center was calculated incorrectly for LatLngBounds with zero span (#11650)

This commit is contained in:
Osana Babayan
2018-04-11 10:00:07 -04:00
committed by GitHub
parent a0d0776925
commit 797486bc5f
2 changed files with 24 additions and 4 deletions

View File

@@ -67,7 +67,7 @@ public class LatLngBounds implements Parcelable {
double latCenter = (this.latitudeNorth + this.latitudeSouth) / 2.0;
double longCenter;
if (this.longitudeEast > this.longitudeWest) {
if (this.longitudeEast >= this.longitudeWest) {
longCenter = (this.longitudeEast + this.longitudeWest) / 2;
} else {
double halfSpan = (GeometryConstants.LONGITUDE_SPAN + this.longitudeEast - this.longitudeWest) / 2.0;
@@ -180,7 +180,7 @@ public class LatLngBounds implements Parcelable {
*/
public double getLongitudeSpan() {
double longSpan = Math.abs(this.longitudeEast - this.longitudeWest);
if (this.longitudeEast > this.longitudeWest) {
if (this.longitudeEast >= this.longitudeWest) {
return longSpan;
}
@@ -191,7 +191,7 @@ public class LatLngBounds implements Parcelable {
static double getLongitudeSpan(final double longEast, final double longWest) {
double longSpan = Math.abs(longEast - longWest);
if (longEast > longWest) {
if (longEast >= longWest) {
return longSpan;
}
@@ -240,7 +240,6 @@ public class LatLngBounds implements Parcelable {
westLon = temp;
}
} else {
lonSpan = GeometryConstants.LONGITUDE_SPAN - lonSpan;
if (westLon < eastLon) {
double temp = eastLon;
eastLon = westLon;

View File

@@ -110,6 +110,14 @@ public class LatLngBoundsTest {
latLngSpan);
}
@Test
public void zeroLongitudeSpan() {
latLngBounds = LatLngBounds.from(10, 10, -10, 10);
LatLngSpan latLngSpan = latLngBounds.getSpan();
assertEquals("LatLngSpan should be shortest distance", new LatLngSpan(20, 0),
latLngSpan);
}
@Test
public void nearDateLineCenter1() {
latLngBounds = LatLngBounds.from(10, -175, -10, 165);
@@ -145,6 +153,19 @@ public class LatLngBoundsTest {
assertEquals("Center should match", new LatLng(0, 90), center);
}
@Test
public void centerForBoundsWithSameLongitude() {
latLngBounds = LatLngBounds.from(10, 10, -10, 10);
LatLng center = latLngBounds.getCenter();
assertEquals("Center should match", new LatLng(0, 10), center);
}
@Test
public void centerForBoundsWithSameLatitude() {
latLngBounds = LatLngBounds.from(10, 10, 10, -10);
LatLng center = latLngBounds.getCenter();
assertEquals("Center should match", new LatLng(10, 0), center);
}
@Test
public void center() {