diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main')
6 files changed, 53 insertions, 87 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java index a8fc58d51c..5e958ff565 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java @@ -232,24 +232,6 @@ public final class CameraPosition implements Parcelable { } /** - * Create Builder from an existing array of doubles. - * <p> - * These values conform to map.ccp representation of a camera position. - * </p> - * - * @param nativeCameraValues Values containing target, bearing, tilt and zoom - */ - public Builder(double[] nativeCameraValues) { - super(); - if (nativeCameraValues != null && nativeCameraValues.length == 5) { - target(new LatLng(nativeCameraValues[0], nativeCameraValues[1]).wrap()); - bearing(MathUtils.convertNativeBearing(nativeCameraValues[2])); - tilt(nativeCameraValues[3]); - zoom(nativeCameraValues[4]); - } - } - - /** * Sets the direction that the camera is pointing in, in degrees clockwise from north. * * @param bearing Bearing diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java index ef8a4d58e8..64b86054a0 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java @@ -2,7 +2,6 @@ package com.mapbox.mapboxsdk.camera; import android.graphics.Point; import android.graphics.PointF; -import android.graphics.RectF; import android.support.annotation.IntDef; import android.support.annotation.NonNull; @@ -11,7 +10,6 @@ import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.Projection; import com.mapbox.mapboxsdk.maps.UiSettings; -import com.mapbox.services.android.telemetry.utils.MathUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -199,17 +197,13 @@ public final class CameraUpdateFactory { static final class CameraBoundsUpdate implements CameraUpdate { private LatLngBounds bounds; - private RectF padding; + private int[] padding; - CameraBoundsUpdate(LatLngBounds bounds, RectF padding) { + CameraBoundsUpdate(LatLngBounds bounds, int[] padding) { this.bounds = bounds; this.padding = padding; } - CameraBoundsUpdate(LatLngBounds bounds, int[] padding) { - this(bounds, new RectF(padding[0], padding[1], padding[2], padding[3])); - } - CameraBoundsUpdate(LatLngBounds bounds, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) { this(bounds, new int[] {paddingLeft, paddingTop, paddingRight, paddingBottom}); } @@ -218,64 +212,13 @@ public final class CameraUpdateFactory { return bounds; } - public RectF getPadding() { + public int[] getPadding() { return padding; } @Override public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { - // Get required objects - Projection projection = mapboxMap.getProjection(); - UiSettings uiSettings = mapboxMap.getUiSettings(); - - // calculate correct padding - int[] mapPadding = mapboxMap.getPadding(); - RectF latLngPadding = getPadding(); - RectF padding = new RectF(latLngPadding.left + mapPadding[0], - latLngPadding.top + mapPadding[1], - latLngPadding.right + mapPadding[2], - latLngPadding.bottom + mapPadding[3]); - - // Calculate the bounds of the possibly rotated shape with respect to the viewport - PointF nePixel = new PointF(-Float.MAX_VALUE, -Float.MAX_VALUE); - PointF swPixel = new PointF(Float.MAX_VALUE, Float.MAX_VALUE); - float viewportHeight = uiSettings.getHeight(); - for (LatLng latLng : getBounds().toLatLngs()) { - PointF pixel = projection.toScreenLocation(latLng); - swPixel.x = Math.min(swPixel.x, pixel.x); - nePixel.x = Math.max(nePixel.x, pixel.x); - swPixel.y = Math.min(swPixel.y, viewportHeight - pixel.y); - nePixel.y = Math.max(nePixel.y, viewportHeight - pixel.y); - } - - // Calculate width/height - float width = nePixel.x - swPixel.x; - float height = nePixel.y - swPixel.y; - - double zoom = 0; - float minScale = 1; - // Calculate the zoom level - if (padding != null) { - float scaleX = (uiSettings.getWidth() - padding.left - padding.right) / width; - float scaleY = (uiSettings.getHeight() - padding.top - padding.bottom) / height; - minScale = scaleX < scaleY ? scaleX : scaleY; - zoom = projection.calculateZoom(minScale); - zoom = MathUtils.clamp(zoom, mapboxMap.getMinZoomLevel(), mapboxMap.getMaxZoomLevel()); - } - - // Calculate the center point - PointF paddedNEPixel = new PointF(nePixel.x + padding.right / minScale, nePixel.y + padding.top / minScale); - PointF paddedSWPixel = new PointF(swPixel.x - padding.left / minScale, swPixel.y - padding.bottom / minScale); - PointF centerPixel = new PointF((paddedNEPixel.x + paddedSWPixel.x) / 2, (paddedNEPixel.y + paddedSWPixel.y) / 2); - centerPixel.y = viewportHeight - centerPixel.y; - LatLng center = projection.fromScreenLocation(centerPixel); - - return new CameraPosition.Builder() - .target(center) - .zoom(zoom) - .tilt(0) - .bearing(0) - .build(); + return mapboxMap.getCameraForLatLngBounds(bounds, padding); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 6edae8b944..e7679a4066 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -36,6 +36,7 @@ import com.mapbox.mapboxsdk.constants.MyBearingTracking; import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings; import com.mapbox.mapboxsdk.style.layers.Filter; import com.mapbox.mapboxsdk.style.layers.Layer; @@ -1505,6 +1506,32 @@ public final class MapboxMap { } // + // LatLngBounds + // + + + /** + * Gets a camera position that would fit a bounds. + * + * @param latLngBounds the bounds to constrain the map with + */ + public CameraPosition getCameraForLatLngBounds(@Nullable LatLngBounds latLngBounds, int[] padding) { + // calculate and set additional bounds padding + int[] mapPadding = getPadding(); + for (int i = 0; i < padding.length; i++) { + padding[i] = mapPadding[i] + padding[i]; + } + projection.setContentPadding(padding, myLocationViewSettings.getPadding()); + + // get padded camera position from LatLngBounds + CameraPosition cameraPosition = nativeMapView.getCameraForLatLngBounds(latLngBounds); + + // reset map padding + setPadding(mapPadding); + return cameraPosition; + } + + // // Padding // @@ -1527,7 +1554,11 @@ public final class MapboxMap { * @param bottom The bottom margin in pixels. */ public void setPadding(int left, int top, int right, int bottom) { - projection.setContentPadding(new int[] {left, top, right, bottom}, myLocationViewSettings.getPadding()); + setPadding(new int[] {left, top, right, bottom}); + } + + private void setPadding(int[] padding) { + projection.setContentPadding(padding, myLocationViewSettings.getPadding()); uiSettings.invalidate(); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index a9394d0b66..fae6311a03 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -17,8 +17,10 @@ import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.Polygon; import com.mapbox.mapboxsdk.annotations.Polyline; +import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.geometry.ProjectedMeters; import com.mapbox.mapboxsdk.storage.FileSource; import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException; @@ -309,6 +311,13 @@ final class NativeMapView { return nativeGetLatLng().wrap(); } + public CameraPosition getCameraForLatLngBounds(LatLngBounds latLngBounds) { + if (isDestroyedOn("getCameraForLatLngBounds")) { + return null; + } + return nativeGetCameraForLatLngBounds(latLngBounds); + } + public void resetPosition() { if (isDestroyedOn("resetPosition")) { return; @@ -717,11 +726,11 @@ final class NativeMapView { nativeFlyTo(angle, center.getLatitude(), center.getLongitude(), duration, pitch, zoom); } - public double[] getCameraValues() { + public CameraPosition getCameraPosition() { if (isDestroyedOn("getCameraValues")) { - return new double[] {}; + return new CameraPosition.Builder().build(); } - return nativeGetCameraValues(); + return nativeGetCameraPosition(); } // Runtime style Api @@ -1005,6 +1014,8 @@ final class NativeMapView { private native LatLng nativeGetLatLng(); + private native CameraPosition nativeGetCameraForLatLngBounds(LatLngBounds latLngBounds); + private native void nativeResetPosition(); private native double nativeGetPitch(); @@ -1095,7 +1106,7 @@ final class NativeMapView { private native void nativeFlyTo(double angle, double latitude, double longitude, long duration, double pitch, double zoom); - private native double[] nativeGetCameraValues(); + private native CameraPosition nativeGetCameraPosition(); private native long nativeGetTransitionDuration(); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java index ec4b903a74..15e0fb8925 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java @@ -135,7 +135,7 @@ final class Transform implements MapView.OnMapChangedListener { @Nullable CameraPosition invalidateCameraPosition() { if (mapView != null) { - cameraPosition = new CameraPosition.Builder(mapView.getCameraValues()).build(); + cameraPosition = mapView.getCameraPosition(); if (onCameraChangeListener != null) { onCameraChangeListener.onCameraChange(this.cameraPosition); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java index 5c9cf93ebc..a0d9646915 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java @@ -110,8 +110,7 @@ public final class CompassView extends AppCompatImageView implements Runnable, F * @param bearing the direction value of the map */ public void update(final double bearing) { - // compass needs reverse bearing #8123 - rotation = (float) -bearing; + rotation = (float) bearing; if (!isEnabled()) { return; |