From 38f636b4dc4dae25f9d34e2fcdfb41f8c9a8d6e4 Mon Sep 17 00:00:00 2001 From: Langston Smith Date: Mon, 1 Apr 2019 09:51:59 -0700 Subject: [PATCH] Adding rotating camera example (#933) * initial additions * added null check and changed to moveCamera() --- .../mapboxandroiddemo/MainActivity.java | 9 + .../src/main/AndroidManifest.xml | 7 + .../camera/SlowlyRotatingCameraActivity.java | 154 ++++++++++++++++++ .../res/layout/activity_camera_slow_spin.xml | 19 +++ .../src/main/res/values/activity_strings.xml | 3 + .../main/res/values/descriptions_strings.xml | 1 + .../src/main/res/values/titles_strings.xml | 1 + .../src/main/res/values/urls_strings.xml | 1 + 8 files changed, 195 insertions(+) create mode 100644 MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/SlowlyRotatingCameraActivity.java create mode 100644 MapboxAndroidDemo/src/main/res/layout/activity_camera_slow_spin.xml diff --git a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java index 992630d3b..3c3fea652 100644 --- a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -37,6 +37,7 @@ import com.mapbox.mapboxandroiddemo.examples.camera.AnimateMapCameraActivity; import com.mapbox.mapboxandroiddemo.examples.camera.BoundingBoxCameraActivity; import com.mapbox.mapboxandroiddemo.examples.camera.RestrictCameraActivity; +import com.mapbox.mapboxandroiddemo.examples.camera.SlowlyRotatingCameraActivity; import com.mapbox.mapboxandroiddemo.examples.dds.AddRainFallStyleActivity; import com.mapbox.mapboxandroiddemo.examples.dds.BathymetryActivity; import com.mapbox.mapboxandroiddemo.examples.dds.ChoroplethJsonVectorMixActivity; @@ -767,6 +768,14 @@ private void initializeModels() { null, R.string.activity_camera_restrict_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( + R.id.nav_camera, + R.string.activity_camera_slowly_rotating_title, + R.string.activity_camera_slowly_rotating_description, + new Intent(MainActivity.this, SlowlyRotatingCameraActivity.class), + null, + R.string.activity_camera_slowly_rotating_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( R.id.nav_offline, R.string.activity_offline_simple_title, diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 4939a7d45..7fc55f9bb 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -642,6 +642,13 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity" /> + + + diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/SlowlyRotatingCameraActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/SlowlyRotatingCameraActivity.java new file mode 100644 index 000000000..729d51b17 --- /dev/null +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/SlowlyRotatingCameraActivity.java @@ -0,0 +1,154 @@ +package com.mapbox.mapboxandroiddemo.examples.camera; + +import android.animation.ValueAnimator; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatActivity; +import android.view.animation.LinearInterpolator; +import android.widget.Toast; + +import com.mapbox.mapboxandroiddemo.R; +import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.maps.Style; + +/** + * Animate the map's camera to slowly spin around a single point ont the map. + */ +public class SlowlyRotatingCameraActivity extends AppCompatActivity implements OnMapReadyCallback, + MapboxMap.OnMapClickListener { + + private static final int DESIRED_NUM_OF_SPINS = 5; + private static final int DESIRED_SECONDS_PER_ONE_FULL_360_SPIN = 40; + private MapView mapView; + private MapboxMap mapboxMap; + private ValueAnimator animator; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Mapbox access token is configured here. This needs to be called either in your application + // object or in the same activity which contains the mapview. + Mapbox.getInstance(this, getString(R.string.access_token)); + + // This contains the MapView in XML and needs to be called after the access token is configured. + setContentView(R.layout.activity_camera_slow_spin); + + mapView = findViewById(R.id.mapView); + mapView.onCreate(savedInstanceState); + mapView.getMapAsync(this); + } + + @Override + public void onMapReady(final MapboxMap mapboxMap) { + mapboxMap.setStyle(Style.SATELLITE_STREETS, new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull Style style) { + SlowlyRotatingCameraActivity.this.mapboxMap = mapboxMap; + + mapboxMap.addOnMapClickListener(SlowlyRotatingCameraActivity.this); + + // Toast instructing user to tap on the map + Toast.makeText( + SlowlyRotatingCameraActivity.this, getString(R.string.rotating_camera_toast_instruction), + Toast.LENGTH_LONG).show(); + + startMapCameraSpinningAnimation(mapboxMap.getCameraPosition().target); + } + }); + } + + @Override + public boolean onMapClick(@NonNull LatLng point) { + startMapCameraSpinningAnimation(point); + return true; + } + + /** + * Set up and start the spin animation. The Android system ValueAnimator emits a new value and that value is + * used as the map camera's new bearing rotation amount. A smooth "new helicopter" type of effect is created + * by using a LinearInterpolator. + * + * @param mapCameraTargetLocation the map location that the map camera should spin around + */ + private void startMapCameraSpinningAnimation(@NonNull final LatLng mapCameraTargetLocation) { + if (animator != null) { + animator.cancel(); + } + animator = ValueAnimator.ofFloat(0, DESIRED_NUM_OF_SPINS * 360); + animator.setDuration( + // Multiplying by 1000 to convert to milliseconds + DESIRED_NUM_OF_SPINS * DESIRED_SECONDS_PER_ONE_FULL_360_SPIN * 1000); + animator.setInterpolator(new LinearInterpolator()); + animator.setStartDelay(1000); + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator valueAnimator) { + // Retrieve the new animation number to use as the map camera bearing value + Float newBearingValue = (Float) valueAnimator.getAnimatedValue(); + + // Use the animation number in a new camera position and then direct the map camera to move to the new position + mapboxMap.moveCamera(CameraUpdateFactory + .newCameraPosition(new CameraPosition.Builder() + .target(new LatLng(mapCameraTargetLocation.getLatitude(), mapCameraTargetLocation.getLongitude())) + .bearing(newBearingValue) + .build())); + } + }); + animator.start(); + } + + @Override + public void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + protected void onStart() { + super.onStart(); + mapView.onStart(); + } + + @Override + protected void onStop() { + super.onStop(); + mapView.onStop(); + } + + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mapboxMap != null) { + mapboxMap.removeOnMapClickListener(this); + } + if (animator != null) { + animator.end(); + } + mapView.onDestroy(); + } +} diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_camera_slow_spin.xml b/MapboxAndroidDemo/src/main/res/layout/activity_camera_slow_spin.xml new file mode 100644 index 000000000..2ab0d6ab7 --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_camera_slow_spin.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml index b5ed0888f..47b10fd7f 100644 --- a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml @@ -309,6 +309,9 @@ Light polar angle Building opacity + + Try tapping on the map and/or adjusting the zoom + Zoom in and out to see the icons change diff --git a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml index ada0aa077..d003a234c 100644 --- a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml @@ -61,6 +61,7 @@ "Animate the map's camera position, tilt, bearing, and zoom." Position the camera so that all the given markers are in view. Prevent a map from being panned to a different place. + Slowly have the camera circle around a single point. Download and view an offline map using the Mapbox Maps SDK. Download, view, navigate to, and delete an offline region. Download, view, navigate to, and delete an offline region. diff --git a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml index a2f755ff2..7d903f1b6 100644 --- a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml @@ -61,6 +61,7 @@ Animate the map camera Fit camera in bounding box Restrict map panning + Rotating camera A simple offline map Offline manager Query a map feature diff --git a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml index 110512522..c9f0260fa 100644 --- a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml @@ -60,6 +60,7 @@ http://i.imgur.com/PN3vyNJ.jpg http://i.imgur.com/A0JL21Q.png http://i.imgur.com/A227BEs.jpg + https://i.imgur.com/uuiOK5D.png http://i.imgur.com/dV4DgDT.png http://i.imgur.com/tDlcNIg.png http://i.imgur.com/tDlcNIg.png