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

Unwrap LatLngBounds during JNI conversion #11807

Merged
merged 2 commits into from
May 2, 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 @@ -46,11 +46,6 @@ public class LatLngBounds implements Parcelable {
this.longitudeWest = westLongitude;
}

LatLngBounds(LatLngBounds latLngBounds) {
this(latLngBounds.latitudeNorth, latLngBounds.longitudeEast,
latLngBounds.latitudeSouth, latLngBounds.longitudeWest);
}

/**
* Returns the world bounds.
*
Expand Down Expand Up @@ -202,25 +197,6 @@ static double getLongitudeSpan(final double longEast, final double longWest) {
return GeometryConstants.LONGITUDE_SPAN - longSpan;
}

/**
* If bounds cross the antimeridian, unwrap west longitude for the shortest path.
*
* @return unwrapped bounds
*/
public LatLngBounds unwrapBounds() {
double unwrapedLonWest = longitudeWest;
if (longitudeEast < longitudeWest) {
if (longitudeWest > 0 && longitudeEast < 0) {
unwrapedLonWest -= GeometryConstants.LONGITUDE_SPAN;
} else if (longitudeWest < 0 && longitudeEast > 0) {
unwrapedLonWest += GeometryConstants.LONGITUDE_SPAN;
}
return unwrapped(latitudeNorth, longitudeEast, latitudeSouth, unwrapedLonWest);
} else {
return new LatLngBounds(this);
}
}

/**
* Validate if LatLngBounds is empty, determined if absolute distance is
*
Expand Down Expand Up @@ -339,20 +315,12 @@ public static LatLngBounds from(
throw new IllegalArgumentException("LatSouth cannot be less than latNorth");
}

return wrapped(latNorth, lonEast, latSouth, lonWest);
}

static LatLngBounds wrapped(double latNorth, double lonEast, double latSouth, double lonWest) {
lonEast = LatLng.wrap(lonEast, GeometryConstants.MIN_LONGITUDE, GeometryConstants.MAX_LONGITUDE);
lonWest = LatLng.wrap(lonWest, GeometryConstants.MIN_LONGITUDE, GeometryConstants.MAX_LONGITUDE);

return new LatLngBounds(latNorth, lonEast, latSouth, lonWest);
}

static LatLngBounds unwrapped(double latNorth, double lonEast, double latSouth, double lonWest) {
return new LatLngBounds(latNorth, lonEast, latSouth, lonWest);
}

private static double lat_(int z, int y) {
double n = Math.PI - 2.0 * Math.PI * y / Math.pow(2.0, z);
return Math.toDegrees(Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1540,12 +1540,7 @@ public boolean isAllowConcurrentMultipleOpenInfoWindows() {
* @param latLngBounds the bounds to constrain the map with
*/
public void setLatLngBoundsForCameraTarget(@Nullable LatLngBounds latLngBounds) {
if (latLngBounds == null) {
nativeMapView.setLatLngBounds(latLngBounds);
} else {
//unwrapping the bounds to generate the right convex hull in core
nativeMapView.setLatLngBounds(latLngBounds.unwrapBounds());
}
nativeMapView.setLatLngBounds(latLngBounds);
}

/**
Expand All @@ -1556,8 +1551,8 @@ public void setLatLngBoundsForCameraTarget(@Nullable LatLngBounds latLngBounds)
* @return the camera position that fits the bounds and padding
*/
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds, int[] padding) {
// get padded camera position from LatLngBounds, unwrapping the bounds to generate the right convex hull in core
return nativeMapView.getCameraForLatLngBounds(latLngBounds.unwrapBounds(), padding);
// get padded camera position from LatLngBounds
return nativeMapView.getCameraForLatLngBounds(latLngBounds, padding);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,23 +565,4 @@ public void testConstructorCheckLatSouthGreaterLatNorth() {
exception.expectMessage("LatSouth cannot be less than latNorth");
LatLngBounds.from(0, 20, 20, 0);
}

@Test
public void testCopyConstructor() {
LatLngBounds bounds = LatLngBounds.from(50, 10, -20, -30);
LatLngBounds copyBounds = new LatLngBounds(bounds);
assertEquals(bounds, copyBounds);
}

@Test
public void testUnwrapBounds() {
LatLngBounds bounds = LatLngBounds.from(16.5, -172.8, -35.127709, 172.6);
LatLngBounds unwrappedBounds = bounds.unwrapBounds();
assertEquals(bounds.getCenter().wrap(), unwrappedBounds.getCenter().wrap());
assertEquals(bounds.getSpan(), unwrappedBounds.getSpan());
assertTrue(unwrappedBounds.getLonEast() < 0 && unwrappedBounds.getLonWest() < 0);

LatLngBounds bounds2 = LatLngBounds.from(16.5, -162.8, -35.127709, -177.4);
assertEquals(bounds2, bounds2.unwrapBounds());
}
}
19 changes: 11 additions & 8 deletions platform/android/src/geometry/lat_lng_bounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ jni::Object<LatLngBounds> LatLngBounds::New(jni::JNIEnv& env, mbgl::LatLngBounds
}

mbgl::LatLngBounds LatLngBounds::getLatLngBounds(jni::JNIEnv& env, jni::Object<LatLngBounds> bounds) {
static auto swLat = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "latitudeSouth");
static auto swLon = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "longitudeWest");
static auto neLat = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "latitudeNorth");
static auto neLon = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "longitudeEast");
return mbgl::LatLngBounds::hull(
{ bounds.Get(env, swLat), bounds.Get(env, swLon) },
{ bounds.Get(env, neLat), bounds.Get(env, neLon) }
);
static auto swLatField = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "latitudeSouth");
static auto swLonField = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "longitudeWest");
static auto neLatField = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "latitudeNorth");
static auto neLonField = LatLngBounds::javaClass.GetField<jni::jdouble>(env, "longitudeEast");

mbgl::LatLng sw = { bounds.Get(env, swLatField), bounds.Get(env, swLonField) };
mbgl::LatLng ne = { bounds.Get(env, neLatField), bounds.Get(env, neLonField) };

sw.unwrapForShortestPath(ne);

return mbgl::LatLngBounds::hull(sw, ne);
}

void LatLngBounds::registerNative(jni::JNIEnv& env) {
Expand Down