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

Commit

Permalink
native should be called only with LatLngUnwrappedBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
osana committed Dec 11, 2018
1 parent ebd87a5 commit 6a9706a
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.LatLngUnwrappedBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.maps.UiSettings;
Expand Down Expand Up @@ -289,7 +290,7 @@ public int[] getPadding() {

@Override
public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
return mapboxMap.getCameraForLatLngBounds(bounds, padding);
return mapboxMap.getCameraForLatLngBounds(LatLngUnwrappedBounds.from(bounds), padding);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package com.mapbox.mapboxsdk.geometry;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.FloatRange;
import android.support.annotation.Keep;
import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.constants.GeometryConstants;

public class LatLngUnwrappedBounds implements Parcelable {

@Keep
private final double latitudeNorth;
@Keep
private final double latitudeSouth;
@Keep
private final double longitudeEast;
@Keep
private final double longitudeWest;

/**
* Constructs a LatLngUnwrappedBounds from doubles representing a LatLng pair.
* <p>
* This values of latNorth and latSouth should be in the range of [-90, 90],
* see {@link GeometryConstants#MIN_LATITUDE} and {@link GeometryConstants#MAX_LATITUDE},
* otherwise IllegalArgumentException will be thrown.
* latNorth should be greater or equal latSouth, otherwise IllegalArgumentException will be thrown.
* lonEast should be greater or equal lonWest, otherwise IllegalArgumentException will be thrown.
* <p>
* This method doesn't recalculate most east or most west boundaries.
* Note that lonEast and lonWest will NOT be wrapped to be in the range of [-180, 180].
*/
public static LatLngUnwrappedBounds from(
@FloatRange(from = GeometryConstants.MIN_LATITUDE, to = GeometryConstants.MAX_LATITUDE) double latNorth,
double lonEast,
@FloatRange(from = GeometryConstants.MIN_LATITUDE, to = GeometryConstants.MAX_LATITUDE) double latSouth,
double lonWest) {

checkParams(latNorth, lonEast, latSouth, lonWest);

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

public static LatLngUnwrappedBounds from(LatLngBounds latLngBounds) {

if (latLngBounds == null) {
return null;
}

if (latLngBounds.getLonEast() < latLngBounds.getLonWest()) {
return new LatLngUnwrappedBounds(latLngBounds.getLatNorth(),
latLngBounds.getLonEast(),
latLngBounds.getLatSouth(),
latLngBounds.getLonWest() - GeometryConstants.LONGITUDE_SPAN);
}

return new LatLngUnwrappedBounds(latLngBounds.getLatNorth(),
latLngBounds.getLonEast(),
latLngBounds.getLatSouth(),
latLngBounds.getLonWest());
}

private static void checkParams(
@FloatRange(from = GeometryConstants.MIN_LATITUDE, to = GeometryConstants.MAX_LATITUDE) double latNorth,
double lonEast,
@FloatRange(from = GeometryConstants.MIN_LATITUDE, to = GeometryConstants.MAX_LATITUDE) double latSouth,
double lonWest) {

if (Double.isNaN(latNorth) || Double.isNaN(latSouth)) {
throw new IllegalArgumentException("latitude must not be NaN");
}

if (Double.isNaN(lonEast) || Double.isNaN(lonWest)) {
throw new IllegalArgumentException("longitude must not be NaN");
}

if (latNorth > GeometryConstants.MAX_LATITUDE || latNorth < GeometryConstants.MIN_LATITUDE
|| latSouth > GeometryConstants.MAX_LATITUDE || latSouth < GeometryConstants.MIN_LATITUDE) {
throw new IllegalArgumentException("latitude must be between -90 and 90");
}

if (latNorth < latSouth) {
throw new IllegalArgumentException("latNorth cannot be less than latSouth");
}

if (lonEast < lonWest) {
throw new IllegalArgumentException("lonEast cannot be less than lonWest");
}
}

/**
* Construct a new LatLngUnwrappedBounds based on its corners, given in NESW
* order.
*
* @param northLatitude Northern Latitude
* @param eastLongitude Eastern Longitude
* @param southLatitude Southern Latitude
* @param westLongitude Western Longitude
*/
@Keep
LatLngUnwrappedBounds(final double northLatitude, final double eastLongitude,
final double southLatitude, final double westLongitude) {
this.latitudeNorth = northLatitude;
this.longitudeEast = eastLongitude;
this.latitudeSouth = southLatitude;
this.longitudeWest = westLongitude;
}

/**
* Get the north latitude value of this bounds.
*
* @return double latitude value for north
*/
public double getLatNorth() {
return this.latitudeNorth;
}

/**
* Get the south latitude value of this bounds.
*
* @return double latitude value for south
*/
public double getLatSouth() {
return this.latitudeSouth;
}

/**
* Get the east longitude value of this bounds.
*
* @return double longitude value for east
*/
public double getLonEast() {
return this.longitudeEast;
}

/**
* Get the west longitude value of this bounds.
*
* @return double longitude value for west
*/
public double getLonWest() {
return this.longitudeWest;
}

/**
* Get the latitude-longitude pair of the south west corner of this bounds.
*
* @return LatLng of the south west corner
*/
@NonNull
public LatLng getSouthWest() {
return new LatLng(latitudeSouth, longitudeWest);
}

/**
* Get the latitude-longitude paur if the north east corner of this bounds.
*
* @return LatLng of the north east corner
*/
@NonNull
public LatLng getNorthEast() {
return new LatLng(latitudeNorth, longitudeEast);
}

/**
* Get the latitude-longitude pair of the south east corner of this bounds.
*
* @return LatLng of the south east corner
*/
@NonNull
public LatLng getSouthEast() {
return new LatLng(latitudeSouth, longitudeEast);
}

/**
* Get the latitude-longitude pair of the north west corner of this bounds.
*
* @return LatLng of the north west corner
*/
@NonNull
public LatLng getNorthWest() {
return new LatLng(latitudeNorth, longitudeWest);
}

/**
* Inner class responsible for recreating Parcels into objects.
*/
public static final Parcelable.Creator<LatLngUnwrappedBounds> CREATOR =
new Parcelable.Creator<LatLngUnwrappedBounds>() {
@Override
public LatLngUnwrappedBounds createFromParcel(@NonNull final Parcel in) {
return readFromParcel(in);
}

@Override
public LatLngUnwrappedBounds[] newArray(final int size) {
return new LatLngUnwrappedBounds[size];
}
};

/**
* Returns a hash code value for the object.
*
* @return the hash code
*/
@Override
public int hashCode() {
return (int) ((latitudeNorth + 90)
+ ((latitudeSouth + 90) * 1000)
+ ((longitudeEast + 180) * 1000000)
+ ((longitudeWest + 180) * 1000000000));
}

/**
* Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.
*
* @return a bitmask indicating the set of special object types marshaled by this Parcelable object instance.
*/
@Override
public int describeContents() {
return 0;
}

/**
* Flatten this object in to a Parcel.
*
* @param out The Parcel in which the object should be written.
* @param flags Additional flags about how the object should be written
*/
@Override
public void writeToParcel(@NonNull final Parcel out, final int flags) {
out.writeDouble(this.latitudeNorth);
out.writeDouble(this.longitudeEast);
out.writeDouble(this.latitudeSouth);
out.writeDouble(this.longitudeWest);
}

private static LatLngUnwrappedBounds readFromParcel(final Parcel in) {
final double northLat = in.readDouble();
final double eastLon = in.readDouble();
final double southLat = in.readDouble();
final double westLon = in.readDouble();
return new LatLngUnwrappedBounds(northLat, eastLon, southLat, westLon);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.LatLngUnwrappedBounds;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.log.Logger;
import com.mapbox.mapboxsdk.offline.OfflineRegionDefinition;
Expand Down Expand Up @@ -1244,66 +1245,67 @@ public void setLatLngBoundsForCameraTarget(@Nullable LatLngBounds latLngBounds)
/**
* Get a camera position that fits a provided bounds and the current camera tilt and bearing.
*
* @param latLngBounds the bounds to set the map with
* @param latLngUnwrappedBounds the bounds to set the map with
* @return the camera position that fits the bounds
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds) {
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngUnwrappedBounds latLngUnwrappedBounds) {
// we use current camera tilt value to provide expected transformations as #11993
return getCameraForLatLngBounds(latLngBounds, new int[] {0, 0, 0, 0});
return getCameraForLatLngBounds(latLngUnwrappedBounds, new int[] {0, 0, 0, 0});
}


/**
* Get a camera position that fits a provided bounds and padding and the current camera tilt and bearing.
*
* @param latLngBounds the bounds to set the map with
* @param latLngUnwrappedBounds the bounds to set the map with
* @param padding the padding to apply to the bounds
* @return the camera position that fits the bounds and padding
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngUnwrappedBounds latLngUnwrappedBounds,
@NonNull @Size(value = 4) int[] padding) {
// we use current camera tilt/bearing value to provide expected transformations as #11993
return getCameraForLatLngBounds(latLngBounds, padding, transform.getRawBearing(), transform.getTilt());
return getCameraForLatLngBounds(latLngUnwrappedBounds,
padding, transform.getRawBearing(), transform.getTilt());
}


/**
* Get a camera position that fits a provided bounds, bearing and tilt.
*
* @param latLngBounds the bounds to set the map with
* @param latLngUnwrappedBounds the bounds to set the map with
* @param bearing the bearing to transform the camera position with
* @param tilt to transform the camera position with
* @return the camera position that fits the bounds and given bearing and tilt
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngUnwrappedBounds latLngUnwrappedBounds,
@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION,
to = MapboxConstants.MAXIMUM_DIRECTION) double bearing,
@FloatRange(from = MapboxConstants.MINIMUM_TILT,
to = MapboxConstants.MAXIMUM_TILT) double tilt) {
return getCameraForLatLngBounds(latLngBounds, new int[] {0, 0, 0, 0}, bearing, tilt);
return getCameraForLatLngBounds(latLngUnwrappedBounds, new int[] {0, 0, 0, 0}, bearing, tilt);
}


/**
* Get a camera position that fits a provided bounds, padding, bearing and tilt.
*
* @param latLngBounds the bounds to set the map with
* @param latLngUnwrappedBounds the bounds to set the map with
* @param padding the padding to apply to the bounds
* @param bearing the bearing to transform the camera position with
* @param tilt to transform the camera position with
* @return the camera position that fits the bounds, bearing and tilt
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngUnwrappedBounds latLngUnwrappedBounds,
@NonNull @Size(value = 4) int[] padding,
@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION,
to = MapboxConstants.MAXIMUM_DIRECTION) double bearing,
@FloatRange(from = MapboxConstants.MINIMUM_TILT,
to = MapboxConstants.MAXIMUM_TILT) double tilt) {
return nativeMapView.getCameraForLatLngBounds(latLngBounds, padding, bearing, tilt);
return nativeMapView.getCameraForLatLngBounds(latLngUnwrappedBounds, padding, bearing, tilt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.mapbox.mapboxsdk.exceptions.CalledFromWorkerThreadException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.LatLngUnwrappedBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
import com.mapbox.mapboxsdk.log.Logger;
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer;
Expand Down Expand Up @@ -214,7 +215,7 @@ public void setLatLngBounds(LatLngBounds latLngBounds) {
if (checkState("setLatLngBounds")) {
return;
}
nativeSetLatLngBounds(latLngBounds);
nativeSetLatLngBounds(LatLngUnwrappedBounds.from(latLngBounds));
}

public void cancelTransitions() {
Expand Down Expand Up @@ -267,7 +268,8 @@ public LatLng getLatLng() {
return nativeGetLatLng().wrap();
}

public CameraPosition getCameraForLatLngBounds(LatLngBounds bounds, int[] padding, double bearing, double tilt) {
public CameraPosition getCameraForLatLngBounds(LatLngUnwrappedBounds bounds,
int[] padding, double bearing, double tilt) {
if (checkState("getCameraForLatLngBounds")) {
return null;
}
Expand Down Expand Up @@ -1024,7 +1026,7 @@ private native void nativeInitialize(NativeMapView nativeMapView,
private native String nativeGetStyleJson();

@Keep
private native void nativeSetLatLngBounds(LatLngBounds latLngBounds);
private native void nativeSetLatLngBounds(LatLngUnwrappedBounds latLngUnwrappedBounds);

@Keep
private native void nativeCancelTransitions();
Expand All @@ -1045,7 +1047,8 @@ private native void nativeInitialize(NativeMapView nativeMapView,
@NonNull
@Keep
private native CameraPosition nativeGetCameraForLatLngBounds(
LatLngBounds latLngBounds, double top, double left, double bottom, double right, double bearing, double tilt);
LatLngUnwrappedBounds latLngUnwrappedBounds,
double top, double left, double bottom, double right, double bearing, double tilt);

@NonNull
@Keep
Expand Down
Loading

0 comments on commit 6a9706a

Please sign in to comment.