From c92d38e6d3dbe91554b54fa2bd7e218ca7914925 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 5 Aug 2015 11:34:59 -0500 Subject: [PATCH] #1940 - Adding enable / disable Compass API --- .../com/mapbox/mapboxgl/views/MapView.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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 c4f5a354842..27085d174c4 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 @@ -127,6 +127,7 @@ public class MapView extends FrameLayout implements LocationListener { private Location mGpsLocation; // Used for compass + private boolean mIsCompassEnabled = true; private ImageView mCompassView; private SensorManager mSensorManager; private Sensor mSensorAccelerometer; @@ -1500,6 +1501,39 @@ private void toggleGps(boolean enableGps) { updateMap(); } + /** + * Gets whether the compass is enabled/disabled. + * @return true if the compass is enabled; false if the compass is disabled. + */ + public boolean isCompassEnabled () { + return mIsCompassEnabled; + } + + /** + * Enables or disables the compass. The compass is an icon on the map that indicates the + * direction of north on the map. If enabled, it is only shown when the camera is tilted or + * rotated away from its default orientation (tilt of 0 and a bearing of 0). When a user clicks + * the compass, the camera orients itself to its default orientation and fades away shortly + * 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. + */ + public void setCompassEnabled (boolean enabled) { + // Set value + this.mIsCompassEnabled = enabled; + + // Toggle UI + if (mIsCompassEnabled && !mCompassView.isShown()) { + mCompassView.setVisibility(View.VISIBLE); + } else if (!mIsCompassEnabled) { + mCompassView.setVisibility(View.GONE); + } + + // Update Map + updateMap(); + } + // This class handles sensor updates to calculate compass bearing private class CompassListener implements SensorEventListener { @@ -1582,7 +1616,11 @@ private void rotateImageView(ImageView imageView, float angle) { // Updates the UI to match the current map's position private void updateMap() { - rotateImageView(mCompassView, (float) getDirection()); + // Using direct access to mIsCompassEnabled instead of isCompassEnabled() for + // small performance boost as this method is called rapidly. + if (mIsCompassEnabled) { + rotateImageView(mCompassView, (float) getDirection()); + } if (isMyLocationEnabled() && mGpsLocation != null) { if (mGpsMarker == null) {