diff options
author | Leith Bade <leith@mapbox.com> | 2015-08-17 15:01:50 +1000 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2015-08-17 17:29:51 +1000 |
commit | e1291fe094acfc93c4582ac6ae98703cc3dcb80c (patch) | |
tree | 4b6b03fab483499b3c05aa2991e3587ef843cf45 | |
parent | 39e1e81b8c8727db8c05b4d099c767acf029b12c (diff) | |
download | qtlocation-mapboxgl-e1291fe094acfc93c4582ac6ae98703cc3dcb80c.tar.gz |
Fix state saving on Android
3 files changed, 208 insertions, 250 deletions
diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java index ae221964b6..a967bc40c6 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java @@ -89,11 +89,14 @@ public class MapView extends FrameLayout implements LocationListener { private static final String STATE_ACCESS_TOKEN = "accessToken"; private static final String STATE_STYLE_CLASSES = "styleClasses"; private static final String STATE_DEFAULT_TRANSITION_DURATION = "defaultTransitionDuration"; + private static final String STATE_COMPASS_ENABLED = "compassEnabled"; + private static final String STATE_MY_LOCATION_ENABLED = "myLocationEnabled"; + private static final String STATE_USER_LOCATION_TRACKING_MODE = "userLocationTrackingMode"; /** * Every annotation that has been added to the map. */ - private List<Annotation> annotations = new ArrayList<>(); + private List<Annotation> mAnnotations = new ArrayList<>(); // // Instance members @@ -124,7 +127,7 @@ public class MapView extends FrameLayout implements LocationListener { private Context mContext; // Used for GPS / Location - private boolean mIsGpsOn = false; + private boolean mIsMyLocationEnabled = false; private LostApiClient mLocationClient; private LocationRequest mLocationRequest; private ImageView mGpsMarker; @@ -133,7 +136,7 @@ public class MapView extends FrameLayout implements LocationListener { public enum UserLocationTrackingMode { NONE, FOLLOW, FOLLOW_BEARING; } - private UserLocationTrackingMode userLocationTrackingMode = UserLocationTrackingMode.FOLLOW; + private UserLocationTrackingMode mUserLocationTrackingMode = UserLocationTrackingMode.FOLLOW; // Used for compass private boolean mIsCompassEnabled = true; @@ -151,13 +154,11 @@ public class MapView extends FrameLayout implements LocationListener { private boolean mCompassValid = false; // Used for map toggle mode -// private FloatingActionButton trackingModeButton; private long t0 = new Date().getTime(); // Used to manage Event Listeners private ArrayList<OnMapChangedListener> mOnMapChangedListener; - // // Properties // @@ -240,28 +241,6 @@ public class MapView extends FrameLayout implements LocationListener { } else { throw new RuntimeException("Need to implement totalMemory on pre-Jelly Bean devices."); } - // Load the attributes - TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MapView, 0, 0); - try { - double centerLatitude = typedArray.getFloat(R.styleable.MapView_centerLatitude, 0.0f); - double centerLongitude = typedArray.getFloat(R.styleable.MapView_centerLongitude, 0.0f); - LatLng centerCoordinate = new LatLng(centerLatitude, centerLongitude); - setCenterCoordinate(centerCoordinate); - setZoomLevel(typedArray.getFloat(R.styleable.MapView_zoomLevel, 0.0f)); // need to set zoom level first because of limitation on rotating when zoomed out - setDirection(typedArray.getFloat(R.styleable.MapView_direction, 0.0f)); - setZoomEnabled(typedArray.getBoolean(R.styleable.MapView_zoomEnabled, true)); - setScrollEnabled(typedArray.getBoolean(R.styleable.MapView_scrollEnabled, true)); - setRotateEnabled(typedArray.getBoolean(R.styleable.MapView_rotateEnabled, true)); - setDebugActive(typedArray.getBoolean(R.styleable.MapView_debugActive, false)); - if (typedArray.getString(R.styleable.MapView_styleUrl) != null) { - setStyleUrl(typedArray.getString(R.styleable.MapView_styleUrl)); - } - if (typedArray.getString(R.styleable.MapView_accessToken) != null) { - setAccessToken(typedArray.getString(R.styleable.MapView_accessToken)); - } - } finally { - typedArray.recycle(); - } // Ensure this view is interactable setClickable(true); @@ -327,6 +306,40 @@ public class MapView extends FrameLayout implements LocationListener { // Setup Support For Listener Tracking // MapView's internal listener is setup in onCreate() mOnMapChangedListener = new ArrayList<OnMapChangedListener>(); + + // Load the attributes + TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MapView, 0, 0); + try { + double centerLatitude = typedArray.getFloat(R.styleable.MapView_centerLatitude, 0.0f); + double centerLongitude = typedArray.getFloat(R.styleable.MapView_centerLongitude, 0.0f); + LatLng centerCoordinate = new LatLng(centerLatitude, centerLongitude); + setCenterCoordinate(centerCoordinate); + setZoomLevel(typedArray.getFloat(R.styleable.MapView_zoomLevel, 0.0f)); // need to set zoom level first because of limitation on rotating when zoomed out + setDirection(typedArray.getFloat(R.styleable.MapView_direction, 0.0f)); + setZoomEnabled(typedArray.getBoolean(R.styleable.MapView_zoomEnabled, true)); + setScrollEnabled(typedArray.getBoolean(R.styleable.MapView_scrollEnabled, true)); + setRotateEnabled(typedArray.getBoolean(R.styleable.MapView_rotateEnabled, true)); + setDebugActive(typedArray.getBoolean(R.styleable.MapView_debugActive, false)); + if (typedArray.getString(R.styleable.MapView_styleUrl) != null) { + setStyleUrl(typedArray.getString(R.styleable.MapView_styleUrl)); + } + if (typedArray.getString(R.styleable.MapView_accessToken) != null) { + setAccessToken(typedArray.getString(R.styleable.MapView_accessToken)); + } + if (typedArray.getString(R.styleable.MapView_styleClasses) != null) { + List<String> styleClasses = Arrays.asList(typedArray.getString(R.styleable.MapView_styleClasses).split("\\s*,\\s*")); + for (String styleClass : styleClasses) { + if (styleClass.length() == 0) { + styleClasses.remove(styleClass); + } + } + setStyleClasses(styleClasses); + } + setCompassEnabled(typedArray.getBoolean(R.styleable.MapView_compassEnabled, true)); + setMyLocationEnabled(typedArray.getBoolean(R.styleable.MapView_myLocationEnabled, false)); + } finally { + typedArray.recycle(); + } } // @@ -348,7 +361,7 @@ public class MapView extends FrameLayout implements LocationListener { long id = mNativeMapView.addMarker(marker); marker.setId(id); // the annotation needs to know its id marker.setMapView(this); // the annotation needs to know which map view it is in - annotations.add(marker); + mAnnotations.add(marker); return marker; } @@ -357,7 +370,7 @@ public class MapView extends FrameLayout implements LocationListener { long id = mNativeMapView.addPolyline(polyline); polyline.setId(id); polyline.setMapView(this); - annotations.add(polyline); + mAnnotations.add(polyline); return polyline; } @@ -366,7 +379,7 @@ public class MapView extends FrameLayout implements LocationListener { long id = mNativeMapView.addPolygon(polygon); polygon.setId(id); polygon.setMapView(this); - annotations.add(polygon); + mAnnotations.add(polygon); return polygon; } @@ -381,14 +394,14 @@ public class MapView extends FrameLayout implements LocationListener { for(int i=0; i < polygons.size(); i++) { polygons.get(i).setId(ids[i]); polygons.get(i).setMapView(this); - annotations.add(polygons.get(i)); + mAnnotations.add(polygons.get(i)); } return polygons; } private void removeAnnotationsWithId(long annotationId){ - for (Iterator<Annotation> iterator = annotations.iterator(); iterator.hasNext();) { + for (Iterator<Annotation> iterator = mAnnotations.iterator(); iterator.hasNext();) { Annotation annotation = iterator.next(); if (annotation.getId() == annotationId) { iterator.remove(); @@ -399,7 +412,7 @@ public class MapView extends FrameLayout implements LocationListener { public void removeAnnotation(Annotation annotation) { long id = annotation.getId(); mNativeMapView.removeAnnotation(id); - annotations.remove(annotation); + mAnnotations.remove(annotation); } public void removeAnnotation(long annotationId) { @@ -408,13 +421,13 @@ public class MapView extends FrameLayout implements LocationListener { } public void removeAnnotations() { - long[] ids = new long[annotations.size()]; - for(int i = 0; i < annotations.size(); i++) { - long id = annotations.get(i).getId(); + long[] ids = new long[mAnnotations.size()]; + for(int i = 0; i < mAnnotations.size(); i++) { + long id = mAnnotations.get(i).getId(); ids[i] = id; } mNativeMapView.removeAnnotations(ids); - annotations.clear(); + mAnnotations.clear(); } // @@ -530,11 +543,11 @@ public class MapView extends FrameLayout implements LocationListener { } public UserLocationTrackingMode getUserLocationTrackingMode() { - return userLocationTrackingMode; + return mUserLocationTrackingMode; } public void setUserLocationTrackingMode(UserLocationTrackingMode userLocationTrackingMode) { - this.userLocationTrackingMode = userLocationTrackingMode; + this.mUserLocationTrackingMode = userLocationTrackingMode; } public boolean isFullyLoaded() { @@ -643,6 +656,9 @@ public class MapView extends FrameLayout implements LocationListener { setStyleClasses(appliedStyleClasses); } mNativeMapView.setDefaultTransitionDuration(savedInstanceState.getLong(STATE_DEFAULT_TRANSITION_DURATION)); + setCompassEnabled(savedInstanceState.getBoolean(STATE_COMPASS_ENABLED)); + setMyLocationEnabled(savedInstanceState.getBoolean(STATE_MY_LOCATION_ENABLED)); + setUserLocationTrackingMode((UserLocationTrackingMode) savedInstanceState.getSerializable(STATE_USER_LOCATION_TRACKING_MODE)); } // Force a check for an access token @@ -673,6 +689,9 @@ public class MapView extends FrameLayout implements LocationListener { outState.putString(STATE_ACCESS_TOKEN, getAccessToken()); outState.putStringArrayList(STATE_STYLE_CLASSES, new ArrayList<>(getStyleClasses())); outState.putLong(STATE_DEFAULT_TRANSITION_DURATION, mNativeMapView.getDefaultTransitionDuration()); + outState.putBoolean(STATE_COMPASS_ENABLED, isCompassEnabled()); + outState.putBoolean(STATE_MY_LOCATION_ENABLED, isMyLocationEnabled()); + outState.putSerializable(STATE_USER_LOCATION_TRACKING_MODE, getUserLocationTrackingMode()); } // Called when we need to clean up @@ -699,6 +718,8 @@ public class MapView extends FrameLayout implements LocationListener { getContext().unregisterReceiver(mConnectivityReceiver); mConnectivityReceiver = null; + toggleGps(!mIsMyLocationEnabled); + mNativeMapView.pause(); } @@ -710,6 +731,8 @@ public class MapView extends FrameLayout implements LocationListener { mConnectivityReceiver = new ConnectivityReceiver(); mContext.registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); + toggleGps(mIsMyLocationEnabled); + mNativeMapView.resume(); } @@ -1502,7 +1525,7 @@ public class MapView extends FrameLayout implements LocationListener { * @return True if the my-location layer is enabled, false otherwise. */ public final boolean isMyLocationEnabled () { - return mIsGpsOn; + return mIsMyLocationEnabled; } /** @@ -1513,7 +1536,9 @@ public class MapView extends FrameLayout implements LocationListener { * @param enabled True to enable; false to disable. */ public final void setMyLocationEnabled (boolean enabled) { + mIsMyLocationEnabled = enabled; toggleGps(enabled); + updateMap(); } /** @@ -1529,10 +1554,8 @@ public class MapView extends FrameLayout implements LocationListener { * @param enableGps true if GPS is to be enabled, false if GPS is to be disabled */ private void toggleGps(boolean enableGps) { - if (enableGps) { - if (!mIsGpsOn) { - mIsGpsOn = true; + if (!mLocationClient.isConnected()) { mGpsLocation = null; mLocationClient.connect(); updateLocation(LocationServices.FusedLocationApi.getLastLocation()); @@ -1541,8 +1564,7 @@ public class MapView extends FrameLayout implements LocationListener { mSensorManager.registerListener(mCompassListener, mSensorMagneticField, SensorManager.SENSOR_DELAY_UI); } } else { - if (mIsGpsOn) { - mIsGpsOn = false; + if (mLocationClient.isConnected()) { LocationServices.FusedLocationApi.removeLocationUpdates(this); mLocationClient.disconnect(); mGpsLocation = null; @@ -1550,7 +1572,6 @@ public class MapView extends FrameLayout implements LocationListener { mSensorManager.unregisterListener(mCompassListener, mSensorMagneticField); } } - updateMap(); } /** @@ -1569,16 +1590,16 @@ public class MapView extends FrameLayout implements LocationListener { * after. If disabled, the compass will never be displayed. * * By default, the compass is enabled - * @param enabled true to enable the compass; false to disable the compass. + * @param compassEnabled true to enable the compass; false to disable the compass. */ - public void setCompassEnabled (boolean enabled) { + public void setCompassEnabled (boolean compassEnabled) { // Set value - this.mIsCompassEnabled = enabled; + this.mIsCompassEnabled = compassEnabled; // Toggle UI - if (mIsCompassEnabled && !mCompassView.isShown()) { + if (mIsCompassEnabled) { mCompassView.setVisibility(View.VISIBLE); - } else if (!mIsCompassEnabled) { + } else { mCompassView.setVisibility(View.GONE); } @@ -1678,7 +1699,7 @@ public class MapView extends FrameLayout implements LocationListener { if (isMyLocationEnabled() && mGpsLocation != null) { if (mGpsMarker == null) { // Setup User Location UI - // NOTE: mIsGpsOn == false to begin with + // NOTE: mIsMyLocationEnabled == false to begin with mGpsMarker = new ImageView(getContext()); mGpsMarker.setImageResource(R.drawable.location_marker); addView(mGpsMarker); @@ -1698,7 +1719,7 @@ public class MapView extends FrameLayout implements LocationListener { mGpsMarker.requestLayout(); // Update direction if tracking mode - if(userLocationTrackingMode == UserLocationTrackingMode.FOLLOW_BEARING && mCompassValid){ + if(mUserLocationTrackingMode == UserLocationTrackingMode.FOLLOW_BEARING && mCompassValid){ // TODO need to do proper filtering (see branch filter-compass) or else map will lock up because of all the compass events long t = new Date().getTime(); if((t-t0)>1000){ @@ -1711,7 +1732,7 @@ public class MapView extends FrameLayout implements LocationListener { // TODO - Too much overhead on main thread. Needs to be refactored before it // can be re-enabled // Update map position if NOT in NONE mode - if (userLocationTrackingMode != UserLocationTrackingMode.NONE) { + if (mUserLocationTrackingMode != UserLocationTrackingMode.NONE) { setCenterCoordinate(new LatLng(mGpsLocation)); } */ diff --git a/android/java/MapboxGLAndroidSDK/src/main/res/values/attrs.xml b/android/java/MapboxGLAndroidSDK/src/main/res/values/attrs.xml index 1e168456be..7148429dbd 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/res/values/attrs.xml +++ b/android/java/MapboxGLAndroidSDK/src/main/res/values/attrs.xml @@ -10,5 +10,8 @@ <attr name="debugActive" format="boolean" /> <attr name="styleUrl" format="string" /> <attr name="accessToken" format="string"/> + <attr name="styleClasses" format="string"/> + <attr name="compassEnabled" format="boolean" /> + <attr name="myLocationEnabled" format="boolean" /> </declare-styleable> </resources> diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java index 0deeeee298..52ab677fd2 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java @@ -4,7 +4,6 @@ import android.graphics.Color; import android.graphics.PointF; import android.location.Location; import android.os.Bundle; -import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; @@ -18,12 +17,8 @@ import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.FrameLayout; -import android.widget.Spinner; import android.widget.TextView; import com.crashlytics.android.Crashlytics; -import com.mapbox.mapboxgl.annotations.Marker; import com.mapbox.mapboxgl.annotations.MarkerOptions; import com.mapbox.mapboxgl.annotations.Polygon; import com.mapbox.mapboxgl.annotations.PolygonOptions; @@ -46,7 +41,8 @@ public class MainActivity extends AppCompatActivity { // // Used for saving instance state - private static final String STATE_IS_GPS_ON = "isGpsOn"; + private static final String STATE_IS_ANNOTATIONS_ON = "isAnnotationsOn"; + private static final String STATE_SELECTED_STYLE = "selectedStyle"; // // Instance members @@ -54,22 +50,17 @@ public class MainActivity extends AppCompatActivity { // Used for the UI private DrawerLayout mDrawerLayout; - private MapView mapView; + private MapView mMapView; private TextView mFpsTextView; - private FrameLayout mMapFrameLayout; - private float mDensity; - private Spinner mClassSpinner; - private ArrayAdapter mOutdoorsClassAdapter; - private ArrayAdapter mSatelliteClassAdapter; + private int mSelectedStyle = R.id.actionStyleMapboxStreets; + NavigationView mNavigationView; // Used for GPS - private FloatingActionButton locationFAB; + private FloatingActionButton mLocationFAB; // Used for Annotations private boolean mIsAnnotationsOn = false; - private Marker marker; - // // Lifecycle events // @@ -80,8 +71,6 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); Fabric.with(this, new Crashlytics()); - mDensity = getResources().getDisplayMetrics().density; - // Load the layout setContentView(R.layout.activity_main); @@ -94,27 +83,23 @@ public class MainActivity extends AppCompatActivity { mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); - NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); - if (navigationView != null) { - setupDrawerContent(navigationView); + mNavigationView = (NavigationView) findViewById(R.id.nav_view); + if (mNavigationView != null) { + setupDrawerContent(mNavigationView); } - mapView = (MapView) findViewById(R.id.mainMapView); + mMapView = (MapView) findViewById(R.id.mainMapView); // Load the access token try { BufferedReader reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.token))); String line = reader.readLine(); - mapView.setAccessToken(line); + mMapView.setAccessToken(line); } catch (IOException e) { Log.e(TAG, "Error loading access token from token.txt: " + e.toString()); } - mapView.onCreate(savedInstanceState); - mapView.setOnFpsChangedListener(new MyOnFpsChangedListener()); - mapView.addOnMapChangedListener(new MyOnMapChangedListener()); - - changeMapStyle(getString(R.string.styleURLMapboxStreets)); - navigationView.getMenu().findItem(R.id.actionStyleMapboxStreets).setChecked(true); + mMapView.onCreate(savedInstanceState); + mMapView.setOnFpsChangedListener(new MyOnFpsChangedListener()); final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { public void onLongPress(final MotionEvent e) { @@ -122,18 +107,18 @@ public class MainActivity extends AppCompatActivity { float y = e.getY(); // flip y direction vertically to match core GL - y = mapView.getHeight() - y; + y = mMapView.getHeight() - y; - LatLng position = mapView.fromScreenLocation(new PointF(x, y)); + LatLng position = mMapView.fromScreenLocation(new PointF(x, y)); - mapView.addMarker(new MarkerOptions() + mMapView.addMarker(new MarkerOptions() .position(position) .title("Dropped Pin") .sprite("default_marker")); } }); - mapView.setOnTouchListener(new View.OnTouchListener() { + mMapView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } @@ -142,34 +127,34 @@ public class MainActivity extends AppCompatActivity { mFpsTextView = (TextView) findViewById(R.id.view_fps); mFpsTextView.setText(""); -// mMapFrameLayout = (FrameLayout) findViewById(R.id.layout_map); - - locationFAB = (FloatingActionButton)findViewById(R.id.locationFAB); - locationFAB.setOnClickListener(new View.OnClickListener() { + mLocationFAB = (FloatingActionButton)findViewById(R.id.locationFAB); + mLocationFAB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toggle GPS position updates - toggleGps(!mapView.isMyLocationEnabled()); - updateMap(); -// Snackbar.make(v, "Snack time!", Snackbar.LENGTH_LONG).show(); + toggleGps(!mMapView.isMyLocationEnabled()); + if (mMapView.isMyLocationEnabled()) { + Location location = mMapView.getMyLocation(); + if (location != null) { + mMapView.setZoomLevel(8); + mMapView.setCenterCoordinate(new LatLng(location)); + } + } } }); -/* - // Add the spinner to select class styles - mClassSpinner = (Spinner) findViewById(R.id.spinner_class); - mOutdoorsClassAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(), - R.array.outdoors_class_list, android.R.layout.simple_spinner_dropdown_item); -*/ - - // Set default UI state - navigationView.getMenu().findItem(R.id.action_compass).setChecked(mapView.isCompassEnabled()); - navigationView.getMenu().findItem(R.id.action_debug).setChecked(mapView.isDebugActive()); - navigationView.getMenu().findItem(R.id.action_markers).setChecked(mIsAnnotationsOn); - + // Restore saved state if (savedInstanceState != null) { - mapView.setMyLocationEnabled(savedInstanceState.getBoolean(STATE_IS_GPS_ON, false)); + mIsAnnotationsOn = savedInstanceState.getBoolean(STATE_IS_ANNOTATIONS_ON); + mSelectedStyle = savedInstanceState.getInt(STATE_SELECTED_STYLE); } + + // Set default UI state + mNavigationView.getMenu().findItem(R.id.action_compass).setChecked(mMapView.isCompassEnabled()); + mNavigationView.getMenu().findItem(R.id.action_debug).setChecked(mMapView.isDebugActive()); + mNavigationView.getMenu().findItem(R.id.action_markers).setChecked(mIsAnnotationsOn); + changeMapStyle(mSelectedStyle); + toggleGps(mMapView.isMyLocationEnabled()); } /** @@ -179,13 +164,13 @@ public class MainActivity extends AppCompatActivity { @Override protected void onStart() { super.onStart(); - mapView.onStart(); + mMapView.onStart(); } @Override protected void onStop() { super.onStop(); - mapView.onStop(); + mMapView.onStop(); } // Called when our app goes into the background @@ -193,10 +178,7 @@ public class MainActivity extends AppCompatActivity { public void onPause() { super.onPause(); - mapView.onPause(); - - // Cancel GPS - toggleGps(false); + mMapView.onPause(); } // Called when our app comes into the foreground @@ -204,16 +186,13 @@ public class MainActivity extends AppCompatActivity { public void onResume() { super.onResume(); - mapView.onResume(); - - // Restart GPS - toggleGps(true); + mMapView.onResume(); } @Override protected void onDestroy() { super.onDestroy(); - mapView.onDestroy(); + mMapView.onDestroy(); } // Called when we need to save instance state @@ -221,8 +200,9 @@ public class MainActivity extends AppCompatActivity { protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - outState.putBoolean(STATE_IS_GPS_ON, mapView.isMyLocationEnabled()); + mMapView.onSaveInstanceState(outState); + outState.putBoolean(STATE_IS_ANNOTATIONS_ON, mIsAnnotationsOn); + outState.putInt(STATE_SELECTED_STYLE, mSelectedStyle); } // Called when the system is running low on memory @@ -230,7 +210,7 @@ public class MainActivity extends AppCompatActivity { public void onLowMemory() { super.onLowMemory(); - mapView.onLowMemory(); + mMapView.onLowMemory(); } // @@ -261,78 +241,88 @@ public class MainActivity extends AppCompatActivity { case R.id.action_debug: // Toggle debug mode - mapView.toggleDebug(); - - menuItem.setChecked(mapView.isDebugActive()); - - // Show the FPS counter - if (mapView.isDebugActive()) { - mFpsTextView.setVisibility(View.VISIBLE); - mFpsTextView.setText(getResources().getString(R.string.label_fps)); - } else { - mFpsTextView.setVisibility(View.INVISIBLE); - } - break; + mMapView.toggleDebug(); + toggleFpsCounter(mMapView.isDebugActive()); + menuItem.setChecked(mMapView.isDebugActive()); + return true; case R.id.action_markers: // Toggle markers toggleAnnotations(!mIsAnnotationsOn); menuItem.setChecked(mIsAnnotationsOn); - break; + return true; case R.id.action_compass: // Toggle compass - mapView.setCompassEnabled(!mapView.isCompassEnabled()); - menuItem.setChecked(mapView.isCompassEnabled()); - break; + mMapView.setCompassEnabled(!mMapView.isCompassEnabled()); + menuItem.setChecked(mMapView.isCompassEnabled()); + return true; /* case R.id.followNone: - mapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.NONE); - break; + mMapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.NONE); + return true; case R.id.followFollow: - mapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.FOLLOW); - break; + mMapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.FOLLOW); + return true; case R.id.followBearing: - mapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.FOLLOW_BEARING); - break; + mMapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.FOLLOW_BEARING); + return true; */ - - case R.id.actionStyleMapboxStreets: - changeMapStyle(getString(R.string.styleURLMapboxStreets)); - menuItem.setChecked(true); - break; - - case R.id.actionStyleEmerald: - changeMapStyle(getString(R.string.styleURLEmerald)); - menuItem.setChecked(true); - break; - - case R.id.actionStyleLight: - changeMapStyle(getString(R.string.styleURLLight)); - menuItem.setChecked(true); - break; - - case R.id.actionStyleDark: - changeMapStyle(getString(R.string.styleURLDark)); - menuItem.setChecked(true); - break; - - case R.id.actionStyleSatellite: - changeMapStyle(getString(R.string.styleURLSatellite)); - menuItem.setChecked(true); - break; + default: + return changeMapStyle(menuItem.getItemId()); } - - return true; } }); } - private void changeMapStyle(@NonNull String styleURL) { - mapView.setStyleUrl(styleURL); + private void toggleFpsCounter(boolean enableFps) { + // Show the FPS counter + if (enableFps) { + mFpsTextView.setVisibility(View.VISIBLE); + mFpsTextView.setText(getResources().getString(R.string.label_fps)); + } else { + mFpsTextView.setVisibility(View.INVISIBLE); + } + } + + private boolean changeMapStyle(int id) { + switch (id) { + case R.id.actionStyleMapboxStreets: + mMapView.setStyleUrl(getString(R.string.styleURLMapboxStreets)); + mNavigationView.getMenu().findItem(id).setChecked(true); + mSelectedStyle = id; + return true; + + case R.id.actionStyleEmerald: + mMapView.setStyleUrl(getString(R.string.styleURLEmerald)); + mNavigationView.getMenu().findItem(id).setChecked(true); + mSelectedStyle = id; + return true; + + case R.id.actionStyleLight: + mMapView.setStyleUrl(getString(R.string.styleURLLight)); + mNavigationView.getMenu().findItem(id).setChecked(true); + mSelectedStyle = id; + return true; + + case R.id.actionStyleDark: + mMapView.setStyleUrl(getString(R.string.styleURLDark)); + mNavigationView.getMenu().findItem(id).setChecked(true); + mSelectedStyle = id; + return true; + + case R.id.actionStyleSatellite: + mMapView.setStyleUrl(getString(R.string.styleURLSatellite)); + mNavigationView.getMenu().findItem(id).setChecked(true); + mSelectedStyle = id; + return true; + + default: + return false; + } } /** @@ -340,22 +330,12 @@ public class MainActivity extends AppCompatActivity { * @param enableGps true if GPS is to be enabled, false if GPS is to be disabled */ private void toggleGps(boolean enableGps) { - if (enableGps) { - if (!mapView.isMyLocationEnabled()) { - mapView.setMyLocationEnabled(enableGps); - Location location = mapView.getMyLocation(); - if (location != null) { - mapView.setZoomLevel(8); - mapView.setCenterCoordinate(new LatLng(location)); - } - locationFAB.setColorFilter(getResources().getColor(R.color.primary)); - } + mMapView.setMyLocationEnabled(enableGps); + mLocationFAB.setColorFilter(getResources().getColor(R.color.primary)); } else { - if (mapView.isMyLocationEnabled()) { - mapView.setMyLocationEnabled(enableGps); - locationFAB.setColorFilter(Color.TRANSPARENT); - } + mMapView.setMyLocationEnabled(enableGps); + mLocationFAB.setColorFilter(Color.TRANSPARENT); } } @@ -371,8 +351,8 @@ public class MainActivity extends AppCompatActivity { addMarkers(); addPolyline(); addPolygon(); - mapView.setZoomLevel(7); - mapView.setCenterCoordinate(new LatLng(38.11727, -122.22839)); + mMapView.setZoomLevel(7); + mMapView.setCenterCoordinate(new LatLng(38.11727, -122.22839)); } } else { if (mIsAnnotationsOn) { @@ -384,25 +364,25 @@ public class MainActivity extends AppCompatActivity { private void addMarkers() { LatLng backLot = new LatLng(38.649441, -121.369064); - MapView map = mapView; - marker = map.addMarker(new MarkerOptions() + MapView map = mMapView; + mMapView.addMarker(new MarkerOptions() .position(backLot) .title("Back Lot") .snippet("The back lot behind my house")); LatLng cheeseRoom = new LatLng(38.531577,-122.010646); - map.addMarker(new MarkerOptions() - .position(cheeseRoom) - .sprite("dog-park-15") - .title("Cheese Room") - .snippet("The only air conditioned room on the property!")); + mMapView.addMarker(new MarkerOptions() + .position(cheeseRoom) + .sprite("dog-park-15") + .title("Cheese Room") + .snippet("The only air conditioned room on the property!")); } private void addPolyline() { try { String geojsonStr = Util.loadStringFromAssets(this, "small_line.geojson"); LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr); - MapView map = mapView; + MapView map = mMapView; Polyline line = map.addPolyline(new PolylineOptions() .add(latLngs) .width(2) @@ -418,7 +398,7 @@ public class MainActivity extends AppCompatActivity { try { geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson"); LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr); - MapView map = mapView; + MapView map = mMapView; ArrayList<PolygonOptions> opts = new ArrayList<PolygonOptions>(); opts.add(new PolygonOptions() .add(latLngs) @@ -432,7 +412,7 @@ public class MainActivity extends AppCompatActivity { } private void removeAnnotations() { - mapView.removeAnnotations(); + mMapView.removeAnnotations(); } // This class handles outdoor class change events @@ -446,13 +426,13 @@ public class MainActivity extends AppCompatActivity { // Day case 0: styleClasses.add("day"); - mapView.setStyleClasses(styleClasses); + mMapView.setStyleClasses(styleClasses); break; // Night case 1: styleClasses.add("night"); - mapView.setStyleClasses(styleClasses); + mMapView.setStyleClasses(styleClasses); break; default: @@ -463,7 +443,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onNothingSelected(AdapterView<?> parent) { - mapView.removeAllStyleClasses(); + mMapView.removeAllStyleClasses(); } } @@ -479,18 +459,18 @@ public class MainActivity extends AppCompatActivity { case 0: styleClasses.add("labels"); styleClasses.add("contours"); - mapView.setStyleClasses(styleClasses); + mMapView.setStyleClasses(styleClasses); break; // Labels Only case 1: styleClasses.add("labels"); - mapView.setStyleClasses(styleClasses); + mMapView.setStyleClasses(styleClasses); break; // No Labels case 2: - mapView.setStyleClasses(styleClasses); + mMapView.setStyleClasses(styleClasses); break; default: @@ -501,7 +481,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onNothingSelected(AdapterView<?> parent) { - mapView.removeAllStyleClasses(); + mMapView.removeAllStyleClasses(); } } @@ -514,50 +494,4 @@ public class MainActivity extends AppCompatActivity { } } - - // Updates the UI to match the current map's position - private void updateMap() { -// rotateImageView(mCompassView, (float) mapView.getDirection()); - -/* - if (mGpsLocation != null) { - mGpsMarker.setVisibility(View.VISIBLE); - LatLng coordinate = new LatLng(mGpsLocation); - PointF screenLocation = mapView.toScreenLocation(coordinate); - - if (mGpsLocation.hasBearing() || mCompassValid) { - mGpsMarker.setImageResource(R.drawable.direction_arrow); - float iconSize = 54.0f * mDensity; - FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int) iconSize, (int) iconSize); - lp.leftMargin = (int) (screenLocation.x - iconSize / 2.0f); - lp.topMargin = mMapFrameLayout.getHeight() - (int) (screenLocation.y + iconSize / 2.0f); - mGpsMarker.setLayoutParams(lp); - float bearing = mGpsLocation.hasBearing() ? mGpsLocation.getBearing() : mCompassBearing; - rotateImageView(mGpsMarker, bearing); - mGpsMarker.requestLayout(); - } else { - mGpsMarker.setImageResource(R.drawable.location_marker); - float iconSize = 27.0f * mDensity; - FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int) iconSize, (int) iconSize); - lp.leftMargin = (int) (screenLocation.x - iconSize / 2.0f); - lp.topMargin = mMapFrameLayout.getHeight() - (int) (screenLocation.y + iconSize / 2.0f); - mGpsMarker.setLayoutParams(lp); - rotateImageView(mGpsMarker, 0.0f); - mGpsMarker.requestLayout(); - } - } else { - mGpsMarker.setVisibility(View.INVISIBLE); - } -*/ - } - - // Called when map state changes - private class MyOnMapChangedListener implements MapView.OnMapChangedListener { - - @Override - public void onMapChanged() { - updateMap(); - } - } - } |