When rotating screen, zoom to the last known location.

This commit is contained in:
Jimmy Shih
2013-04-11 18:35:33 -07:00
parent 5b28f4b1c9
commit 179229821b
@@ -95,10 +95,31 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
// Current location // Current location
private Location currentLocation; private Location currentLocation;
// True to keep the currentLocation visible /**
* True to continue keeping the current location visible on the screen.
* <p>
* Set to true when <br>
* 1. user clicks on the my location button <br>
* Set to false when <br>
* 1. showing a marker <br>
* 2. user manually zooms/pans
*/
private boolean keepCurrentLocationVisible; private boolean keepCurrentLocationVisible;
// True to zoom to currentLocation when it is available /**
* True to zoom/center the current location when it is available.
* <p>
* Set to true when <br>
* 1. user clicks on the my location button <br>
* 2. first location during a recording <br>
* Set to false when <br>
* 1. showing a marker <br>
* 2. user manually zooms/pans <br>
* 3. after zooming to the current location <br>
* <p>
* The last one is to support the use case of zooming only once. After zoom,
* set it to false. E.g., only zoom to the first location during a recording.
*/
private boolean zoomToCurrentLocation; private boolean zoomToCurrentLocation;
private OnLocationChangedListener onLocationChangedListener; private OnLocationChangedListener onLocationChangedListener;
@@ -221,8 +242,8 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
keepCurrentLocationVisible = savedInstanceState.getBoolean( keepCurrentLocationVisible = savedInstanceState.getBoolean(
KEEP_CURRENT_LOCATION_VISIBLE_KEY, false); KEEP_CURRENT_LOCATION_VISIBLE_KEY, false);
zoomToCurrentLocation = savedInstanceState.getBoolean(ZOOM_TO_CURRENT_LOCATION_KEY, false); zoomToCurrentLocation = savedInstanceState.getBoolean(ZOOM_TO_CURRENT_LOCATION_KEY, false);
currentLocation = (Location) savedInstanceState.getParcelable(CURRENT_LOCATION_KEY); Location location = (Location) savedInstanceState.getParcelable(CURRENT_LOCATION_KEY);
updateCurrentLocation(); setCurrentLocation(location);
} }
} }
@@ -236,6 +257,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
ApiAdapterFactory.getApiAdapter().invalidMenu(getActivity()); ApiAdapterFactory.getApiAdapter().invalidMenu(getActivity());
} }
resumeTrackDataHub(); resumeTrackDataHub();
updateCurrentLocation();
} }
@Override @Override
@@ -348,7 +370,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
} }
boolean myLocationEnabled = true; boolean myLocationEnabled = true;
if (state == LocationState.DISABLED) { if (state == LocationState.DISABLED) {
currentLocation = null; setCurrentLocation(null);
myLocationEnabled = false; myLocationEnabled = false;
} }
googleMap.setMyLocationEnabled(myLocationEnabled); googleMap.setMyLocationEnabled(myLocationEnabled);
@@ -415,14 +437,11 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
@Override @Override
public void onLocationChanged(final Location location) { public void onLocationChanged(final Location location) {
if (isResumed()) { if (isResumed()) {
if (isSelectedTrackRecording() && currentLocation == null && location != null) { setCurrentLocation(location);
zoomToCurrentLocation = true;
}
currentLocation = location;
updateCurrentLocation(); updateCurrentLocation();
} }
} }
@Override @Override
public void onHeadingChanged(double heading) { public void onHeadingChanged(double heading) {
// We don't care. // We don't care.
@@ -586,6 +605,19 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
} }
} }
/**
* Sets the current location.
*
* @param location the location
*/
private void setCurrentLocation(Location location) {
// If recording, zoom to the first location
if (isSelectedTrackRecording() && currentLocation == null && location != null) {
zoomToCurrentLocation = true;
}
currentLocation = location;
}
/** /**
* Updates the current location and zoom to it if necessary. * Updates the current location and zoom to it if necessary.
*/ */