Skip to content

Commit

Permalink
[android] - keep location tracking mode after screen rotation (mapbox…
Browse files Browse the repository at this point in the history
  • Loading branch information
paczos committed Jun 5, 2017
1 parent da53200 commit 5ea7aab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ void onSaveInstanceState(Bundle outState) {
*/
void onRestoreInstanceState(Bundle savedInstanceState) {
final CameraPosition cameraPosition = savedInstanceState.getParcelable(MapboxConstants.STATE_CAMERA_POSITION);
if (cameraPosition != null) {
moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder(cameraPosition).build()));
}

uiSettings.onRestoreInstanceState(savedInstanceState);
trackingSettings.onRestoreInstanceState(savedInstanceState);

if (cameraPosition != null) {
moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder(cameraPosition).build()), null, trackingSettings.isLocationTrackingDisabled());
}

nativeMapView.setDebug(savedInstanceState.getBoolean(MapboxConstants.STATE_DEBUG_ACTIVE));

final String styleUrl = savedInstanceState.getString(MapboxConstants.STATE_STYLE_URL);
Expand Down Expand Up @@ -643,7 +645,29 @@ public final void moveCamera(final CameraUpdate update, final MapboxMap.Cancelab
new Handler().post(new Runnable() {
@Override
public void run() {
transform.moveCamera(MapboxMap.this, update, callback);
transform.moveCamera(MapboxMap.this, update, callback, true);
// MapChange.REGION_DID_CHANGE_ANIMATED is not called for `jumpTo`
// invalidate camera position to provide OnCameraChange event.
invalidateCameraPosition();
}
});
}

/**
* Repositions the camera according to the instructions defined in the update.
* The move is instantaneous, and a subsequent getCameraPosition() will reflect the new position.
* See CameraUpdateFactory for a set of updates.
*
* @param update The change that should be applied to the camera
* @param callback the callback to be invoked when an animation finishes or is canceled
* @param canDismissTracking you can specify whether this move can reset location tracking or not
*/
@UiThread
public final void moveCamera(final CameraUpdate update, final MapboxMap.CancelableCallback callback, final boolean canDismissTracking) {
new Handler().post(new Runnable() {
@Override
public void run() {
transform.moveCamera(MapboxMap.this, update, callback, canDismissTracking);
// MapChange.REGION_DID_CHANGE_ANIMATED is not called for `jumpTo`
// invalidate camera position to provide OnCameraChange event.
invalidateCameraPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class Transform implements MapView.OnMapChangedListener {
void initialise(@NonNull MapboxMap mapboxMap, @NonNull MapboxMapOptions options) {
CameraPosition position = options.getCamera();
if (position != null && !position.equals(CameraPosition.DEFAULT)) {
moveCamera(mapboxMap, CameraUpdateFactory.newCameraPosition(position), null);
moveCamera(mapboxMap, CameraUpdateFactory.newCameraPosition(position), null, true);
}
setMinZoom(options.getMinZoomPreference());
setMaxZoom(options.getMaxZoomPreference());
Expand Down Expand Up @@ -91,10 +91,10 @@ public void onMapChanged(@MapView.MapChange int change) {
}

@UiThread
final void moveCamera(MapboxMap mapboxMap, CameraUpdate update, MapboxMap.CancelableCallback callback) {
final void moveCamera(MapboxMap mapboxMap, CameraUpdate update, MapboxMap.CancelableCallback callback, boolean canDismissTracking) {
CameraPosition cameraPosition = update.getCameraPosition(mapboxMap);
if (!cameraPosition.equals(this.cameraPosition)) {
trackingSettings.resetTrackingModesIfRequired(this.cameraPosition, cameraPosition, false);
trackingSettings.resetTrackingModesIfRequired(this.cameraPosition, cameraPosition, !canDismissTracking);
cancelTransitions();
cameraChangeDispatcher.onCameraMoveStarted(OnCameraMoveStartedListener.REASON_API_ANIMATION);
mapView.jumpTo(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt, cameraPosition.zoom);
Expand Down

0 comments on commit 5ea7aab

Please sign in to comment.