6453 restore tilt after orientation (#6491)
* [android] - correct tilt value when restoring activity or creating from xml * correct values requesting camera position adhoc * updated logic change in unit tests
This commit is contained in:
@@ -3,11 +3,9 @@ package com.mapbox.mapboxsdk.camera;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.FloatRange;
|
||||
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MathConstants;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.utils.MathUtils;
|
||||
|
||||
@@ -125,7 +123,6 @@ public final class CameraPosition implements Parcelable {
|
||||
private LatLng target = null;
|
||||
private double tilt = -1;
|
||||
private double zoom = -1;
|
||||
private boolean isRadian;
|
||||
|
||||
/**
|
||||
* Creates an empty builder.
|
||||
@@ -134,15 +131,6 @@ public final class CameraPosition implements Parcelable {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a builder for building CameraPosition objects using radians.
|
||||
*
|
||||
* @param isRadian true if heading is in radians
|
||||
*/
|
||||
public Builder(boolean isRadian) {
|
||||
this.isRadian = isRadian;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Builder with an existing CameraPosition data.
|
||||
*
|
||||
@@ -170,7 +158,7 @@ public final class CameraPosition implements Parcelable {
|
||||
double lat = typedArray.getFloat(R.styleable.MapView_center_latitude, 0.0f);
|
||||
double lng = typedArray.getFloat(R.styleable.MapView_center_longitude, 0.0f);
|
||||
this.target = new LatLng(lat, lng);
|
||||
this.tilt = typedArray.getFloat(R.styleable.MapView_tilt, 0.0f);
|
||||
this.tilt = Math.toRadians(typedArray.getFloat(R.styleable.MapView_tilt, 0.0f));
|
||||
this.zoom = typedArray.getFloat(R.styleable.MapView_zoom, 0.0f);
|
||||
}
|
||||
}
|
||||
@@ -203,7 +191,7 @@ public final class CameraPosition implements Parcelable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Builder from an exisiting array of doubles.
|
||||
* Create Builder from an existing array of doubles.
|
||||
*
|
||||
* @param values Values containing target, bearing, tilt and zoom
|
||||
*/
|
||||
@@ -212,7 +200,7 @@ public final class CameraPosition implements Parcelable {
|
||||
if (values != null && values.length == 5) {
|
||||
this.target = new LatLng(values[0], values[1]);
|
||||
this.bearing = (float) values[2];
|
||||
this.tilt = (float) values[3];
|
||||
this.tilt = Math.toRadians(values[3]);
|
||||
this.zoom = (float) values[4];
|
||||
}
|
||||
}
|
||||
@@ -224,12 +212,7 @@ public final class CameraPosition implements Parcelable {
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder bearing(double bearing) {
|
||||
if (isRadian) {
|
||||
this.bearing = bearing;
|
||||
} else {
|
||||
// converting degrees to radians
|
||||
this.bearing = (float) (-bearing * MathConstants.DEG2RAD);
|
||||
}
|
||||
this.bearing = bearing;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -259,14 +242,8 @@ public final class CameraPosition implements Parcelable {
|
||||
* @param tilt Tilt value
|
||||
* @return Builder
|
||||
*/
|
||||
@FloatRange(from = 0.0, to = 60.0)
|
||||
public Builder tilt(double tilt) {
|
||||
if (isRadian) {
|
||||
this.tilt = tilt;
|
||||
} else {
|
||||
// converting degrees to radians
|
||||
this.tilt = (float) (MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT) * MathConstants.DEG2RAD);
|
||||
}
|
||||
this.tilt = (float) Math.toRadians(MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -184,8 +184,8 @@ public final class CameraUpdateFactory {
|
||||
public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
|
||||
CameraPosition previousPosition = mapboxMap.getCameraPosition();
|
||||
if (target == null) {
|
||||
return new CameraPosition.Builder(true)
|
||||
.tilt(tilt)
|
||||
return new CameraPosition.Builder()
|
||||
.tilt(Math.toDegrees(tilt))
|
||||
.zoom(zoom)
|
||||
.bearing(bearing)
|
||||
.target(previousPosition.target)
|
||||
@@ -310,8 +310,8 @@ public final class CameraUpdateFactory {
|
||||
.bearing(previousPosition.bearing)
|
||||
.build();
|
||||
} else {
|
||||
return new CameraPosition.Builder(true)
|
||||
.tilt(previousPosition.tilt)
|
||||
return new CameraPosition.Builder()
|
||||
.tilt(Math.toDegrees(previousPosition.tilt))
|
||||
.zoom(previousPosition.zoom)
|
||||
.bearing(previousPosition.bearing)
|
||||
.target(previousPosition.target)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.constants;
|
||||
|
||||
/**
|
||||
* MathConstants exposes math related constant values
|
||||
*/
|
||||
public class MathConstants {
|
||||
|
||||
/**
|
||||
* Constant used to convert degrees to radials
|
||||
*/
|
||||
public static final double DEG2RAD = (Math.PI / 180.0);
|
||||
|
||||
/**
|
||||
* Constant used to convert radials to degrees
|
||||
*/
|
||||
public static final double RAD2DEG = (180.0 / Math.PI);
|
||||
|
||||
/**
|
||||
* The number PI
|
||||
*/
|
||||
public static final double PI = Math.PI;
|
||||
}
|
||||
@@ -5,11 +5,8 @@ import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.GeoConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MathConstants;
|
||||
import com.mapbox.mapboxsdk.utils.MathUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* A geographical location which contains a single latitude, longitude pair, with
|
||||
* optional altitude.
|
||||
@@ -188,10 +185,10 @@ public class LatLng implements ILatLng, Parcelable {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
final double a1 = MathConstants.DEG2RAD * this.latitude;
|
||||
final double a2 = MathConstants.DEG2RAD * this.longitude;
|
||||
final double b1 = MathConstants.DEG2RAD * other.getLatitude();
|
||||
final double b2 = MathConstants.DEG2RAD * other.getLongitude();
|
||||
final double a1 = Math.toRadians(this.latitude);
|
||||
final double a2 = Math.toRadians(this.longitude);
|
||||
final double b1 = Math.toRadians(other.getLatitude());
|
||||
final double b2 = Math.toRadians(other.getLongitude());
|
||||
|
||||
final double cosa1 = Math.cos(a1);
|
||||
final double cosb1 = Math.cos(b1);
|
||||
|
||||
@@ -382,7 +382,7 @@ public class MapView extends FrameLayout {
|
||||
// Get previous camera position
|
||||
CameraPosition cameraPosition = savedInstanceState.getParcelable(MapboxConstants.STATE_CAMERA_POSITION);
|
||||
if (cameraPosition != null) {
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder(cameraPosition).build()));
|
||||
}
|
||||
|
||||
UiSettings uiSettings = mapboxMap.getUiSettings();
|
||||
|
||||
@@ -5,7 +5,6 @@ import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MathConstants;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.utils.MathUtils;
|
||||
import com.mapbox.mapboxsdk.utils.MockParcel;
|
||||
@@ -59,7 +58,7 @@ public class CameraPositionTest {
|
||||
CameraPosition cameraPosition = new CameraPosition.Builder(typedArray).build();
|
||||
assertEquals("bearing should match", bearing, cameraPosition.bearing, DELTA);
|
||||
assertEquals("latlng should match", new LatLng(latitude, longitude), cameraPosition.target);
|
||||
assertEquals("tilt should match", tilt, cameraPosition.tilt, DELTA);
|
||||
assertEquals("tilt should match", Math.toRadians(tilt), cameraPosition.tilt, DELTA);
|
||||
assertEquals("zoom should match", zoom, cameraPosition.zoom, DELTA);
|
||||
}
|
||||
|
||||
@@ -75,7 +74,7 @@ public class CameraPositionTest {
|
||||
CameraPosition cameraPosition = new CameraPosition.Builder(cameraVars).build();
|
||||
assertEquals("bearing should match", bearing, cameraPosition.bearing, DELTA);
|
||||
assertEquals("latlng should match", new LatLng(latitude, longitude), cameraPosition.target);
|
||||
assertEquals("tilt should match", tilt, cameraPosition.tilt, DELTA);
|
||||
assertEquals("tilt should match", Math.toRadians(tilt), cameraPosition.tilt, DELTA);
|
||||
assertEquals("zoom should match", zoom, cameraPosition.zoom, DELTA);
|
||||
}
|
||||
|
||||
@@ -93,40 +92,6 @@ public class CameraPositionTest {
|
||||
assertEquals("hashCode should match", -1007681505, cameraPosition.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRadianBuilder() {
|
||||
LatLng latLng = new LatLng(1, 2);
|
||||
CameraPosition.Builder builder = new CameraPosition.Builder(true);
|
||||
builder.target(latLng);
|
||||
builder.zoom(3);
|
||||
builder.tilt(4);
|
||||
builder.bearing(5);
|
||||
CameraPosition cameraPosition = new CameraPosition(latLng, 3, 4, 5);
|
||||
assertEquals("CameraPosition should match", cameraPosition, builder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDegreesRadianBuilder() {
|
||||
LatLng latLng = new LatLng(1, 2);
|
||||
float tilt = 4;
|
||||
float bearing = 5;
|
||||
float bearingRadian = (float) (-bearing * MathConstants.DEG2RAD);
|
||||
float tiltRadian = (float) (MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT) * MathConstants.DEG2RAD);
|
||||
|
||||
CameraPosition.Builder degreeBuilder = new CameraPosition.Builder(false);
|
||||
degreeBuilder.target(latLng);
|
||||
degreeBuilder.zoom(3);
|
||||
degreeBuilder.tilt(tilt);
|
||||
degreeBuilder.bearing(bearing);
|
||||
|
||||
CameraPosition.Builder radianBuilder = new CameraPosition.Builder(true);
|
||||
radianBuilder.target(latLng);
|
||||
radianBuilder.zoom(3);
|
||||
radianBuilder.tilt(tiltRadian);
|
||||
radianBuilder.bearing(bearingRadian);
|
||||
assertEquals("CameraPosition should match", radianBuilder.build(), degreeBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoomUpdateBuilder() {
|
||||
float zoomLevel = 5;
|
||||
|
||||
Reference in New Issue
Block a user