Fix race conditions with GoogleMap

This commit is contained in:
Jimmy Shih
2012-10-24 14:38:51 -07:00
parent 9e30280e4e
commit 41cb741883
3 changed files with 21 additions and 28 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ limitations under the License.
--> -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto" android:installLocation="auto"
android:versionCode="57" android:versionCode="58"
android:versionName="2.0.3" android:versionName="2.0.3"
package="com.google.android.maps.mytracks" > package="com.google.android.maps.mytracks" >
<!-- Permission to initialize services --> <!-- Permission to initialize services -->
+5 -3
View File
@@ -1712,9 +1712,11 @@ limitations under the License.
Auto-resume Auto-resume
</string> </string>
<string name="settings_recording_auto_resume_track_timeout_summary" <string name="settings_recording_auto_resume_track_timeout_summary"
translation_description="In the 'Recording' settings, the summary for the 'Auto-resume' translation_description="In the 'Recording' settings, the summary for the 'Auto-resume' setting.
setting."> Occasionaly, phones will reboot due to crashes or users would need to change battery during a
Track is resumed after reboot if the last recording time is within this. long recording. My Tracks will resume a recording after reboot if it was recording beforehand
and that the last recorded time is recent, within certain minutes.">
Recording is resumed after reboot if the last recorded time is within this.
</string> </string>
<string name="settings_recording_default_activity_title" <string name="settings_recording_default_activity_title"
translation_description="In the 'Recording' settings, the title to configure the default translation_description="In the 'Recording' settings, the title to configure the default
@@ -84,13 +84,12 @@ public class MyTracksMapFragment extends SupportMapFragment
private static final float DEFAULT_ZOOM_LEVEL = 18f; private static final float DEFAULT_ZOOM_LEVEL = 18f;
// If current zoom level is above this, no need to zoom to DEFAULT_ZOOM_LEVEL
private static final float MIN_ALLOWED_ZOOM_LEVEL = 10f;
// Google's latitude and longitude // Google's latitude and longitude
private static final double DEFAULT_LATITUDE = 37.423; private static final double DEFAULT_LATITUDE = 37.423;
private static final double DEFAULT_LONGITUDE = -122.084; private static final double DEFAULT_LONGITUDE = -122.084;
private static final int MAP_VIEW_PADDING = 32;
private TrackDataHub trackDataHub; private TrackDataHub trackDataHub;
// Current location // Current location
@@ -118,6 +117,7 @@ public class MyTracksMapFragment extends SupportMapFragment
// UI elements // UI elements
private GoogleMap googleMap; private GoogleMap googleMap;
private MapOverlay mapOverlay; private MapOverlay mapOverlay;
private View mapView;
private ImageButton myLocationImageButton; private ImageButton myLocationImageButton;
private TextView messageTextView; private TextView messageTextView;
@@ -133,10 +133,10 @@ public class MyTracksMapFragment extends SupportMapFragment
@Override @Override
public View onCreateView( public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState); mapView = super.onCreateView(inflater, container, savedInstanceState);
View layout = inflater.inflate(R.layout.map, container, false); View layout = inflater.inflate(R.layout.map, container, false);
RelativeLayout mapContainer = (RelativeLayout) layout.findViewById(R.id.map_container); RelativeLayout mapContainer = (RelativeLayout) layout.findViewById(R.id.map_container);
mapContainer.addView(view, 0); mapContainer.addView(mapView, 0);
myLocationImageButton = (ImageButton) layout.findViewById(R.id.map_my_location); myLocationImageButton = (ImageButton) layout.findViewById(R.id.map_my_location);
myLocationImageButton.setOnClickListener(new View.OnClickListener() { myLocationImageButton.setOnClickListener(new View.OnClickListener() {
@@ -191,7 +191,7 @@ public class MyTracksMapFragment extends SupportMapFragment
@Override @Override
public void onCameraChange(CameraPosition cameraPosition) { public void onCameraChange(CameraPosition cameraPosition) {
if (keepCurrentLocationVisible && currentLocation != null if (isResumed() && keepCurrentLocationVisible && currentLocation != null
&& !isLocationVisible(currentLocation)) { && !isLocationVisible(currentLocation)) {
keepCurrentLocationVisible = false; keepCurrentLocationVisible = false;
zoomToCurrentLocation = false; zoomToCurrentLocation = false;
@@ -563,7 +563,7 @@ public class MyTracksMapFragment extends SupportMapFragment
if (zoomToCurrentLocation if (zoomToCurrentLocation
|| (keepCurrentLocationVisible && !isLocationVisible(currentLocation))) { || (keepCurrentLocationVisible && !isLocationVisible(currentLocation))) {
LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
googleMap.animateCamera(CameraUpdates.newLatLngZoom(latLng, getDefaultZoomLevel())); googleMap.animateCamera(CameraUpdates.newLatLngZoom(latLng, DEFAULT_ZOOM_LEVEL));
zoomToCurrentLocation = false; zoomToCurrentLocation = false;
} }
}; };
@@ -582,6 +582,10 @@ public class MyTracksMapFragment extends SupportMapFragment
return; return;
} }
if (mapView == null || mapView.getWidth() == 0 || mapView.getHeight() == 0) {
return;
}
TripStatistics tripStatistics = currentTrack.getTripStatistics(); TripStatistics tripStatistics = currentTrack.getTripStatistics();
int latitudeSpanE6 = tripStatistics.getTop() - tripStatistics.getBottom(); int latitudeSpanE6 = tripStatistics.getTop() - tripStatistics.getBottom();
int longitudeSpanE6 = tripStatistics.getRight() - tripStatistics.getLeft(); int longitudeSpanE6 = tripStatistics.getRight() - tripStatistics.getLeft();
@@ -592,7 +596,8 @@ public class MyTracksMapFragment extends SupportMapFragment
LatLng northEast = new LatLng( LatLng northEast = new LatLng(
tripStatistics.getTopDegrees(), tripStatistics.getRightDegrees()); tripStatistics.getTopDegrees(), tripStatistics.getRightDegrees());
LatLngBounds bounds = Model.newLatLngBounds(southWest, northEast); LatLngBounds bounds = Model.newLatLngBounds(southWest, northEast);
CameraUpdate cameraUpdate = CameraUpdates.newLatLngBounds(bounds, 32); CameraUpdate cameraUpdate = CameraUpdates.newLatLngBounds(
bounds, mapView.getWidth(), mapView.getHeight(), MAP_VIEW_PADDING);
googleMap.moveCamera(cameraUpdate); googleMap.moveCamera(cameraUpdate);
} }
} }
@@ -618,7 +623,7 @@ public class MyTracksMapFragment extends SupportMapFragment
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
keepCurrentLocationVisible = false; keepCurrentLocationVisible = false;
zoomToCurrentLocation = false; zoomToCurrentLocation = false;
CameraUpdate cameraUpdate = CameraUpdates.newLatLngZoom(latLng, getDefaultZoomLevel()); CameraUpdate cameraUpdate = CameraUpdates.newLatLngZoom(latLng, DEFAULT_ZOOM_LEVEL);
googleMap.moveCamera(cameraUpdate); googleMap.moveCamera(cameraUpdate);
} }
} }
@@ -638,20 +643,6 @@ public class MyTracksMapFragment extends SupportMapFragment
} }
} }
/**
* Gets the default zoom level. Needs to run on the UI thread.
*/
private float getDefaultZoomLevel() {
if (googleMap == null) {
return DEFAULT_ZOOM_LEVEL;
}
float zoom = googleMap.getCameraPosition().getZoom();
if (zoom <= MIN_ALLOWED_ZOOM_LEVEL) {
return DEFAULT_ZOOM_LEVEL;
}
return zoom;
}
/** /**
* Returns true if the location is visible. Needs to run on the UI thread. * Returns true if the location is visible. Needs to run on the UI thread.
* *