Examples
Getting started (3)Create a simple map viewDynamically build a map viewSupport map fragmentDynamic styling (30)HillshadingAdd a vector tile sourceAdd a new layer below labelsAnimate marker positionAdjust a layer's opacityShow time lapseAnimated image source (GIF)Icon update based on API responseChange a layer's colorChange a map's languageColor dependent on zoom levelClick to add photoIndoor MapMarker following routeStyle with missing iconSymbol layer iconsPicture in picturePulsing layer opacityAdd a WMS sourceText anchor positionButton interaction stylingOpacity fadeShow and hide layersSymbolLayer iconsAdjust text labelsMultiple text formatsTransparent render surfaceUse an image sourceAnimated icon movementVariable label placementExtrusions (5)Adjust light location and colorDisplay 3D building height based on vector dataExtrude polygons for 3D indoor mappingRotate and tilt with 3D buildingsUse GeoJSON data to set extrusion heightCamera (5)Animate the map cameraFit camera in bounding boxRestrict map panningRotating cameraZoom to show a cluster's leavesData visualization (33)Baseball spray chartDisplay water depthBiometric fingerprintCalendar integrationCircle icon toggleCircleLayer clustersCircle radiusCircle to icon transitionCreate a line layerCreate hotspots from pointsData time lapseShow heatmap dataDraw a GeoJSON lineDraw a polygon with holesDraw a polygonDraw multiple geometriesJoin local JSON data with vector tile geometriesLine gradientLine behind moving iconMultiple expressionsIcon setting based on Feature propertyOutlined polygon holeSatellite land selectShared preferencesSpinning iconStyle circles categoricallyStyle lines using an identity property functionToggle collision detectionSymbolLayer clusteringSymbol layer info windowZoom-based icon switchUpdate a choropleth layer by zoom levelStyling heatmapsOffline (3)Cache managementA simple offline mapOffline managerQuerying the map (3)Building outlineQuery a map featureSelect a buildingMap style (4)Style attributionLocal style or custom raster styleDefault stylesMapbox Studio styleUser interaction (12)Click on single layerFeature countDrawing search areaHighlighted lineSymbol layer icon size changeInset mapLocation pickerBackground fogRecyclerView DirectionsSearch again in an areaSnaking directionsRecyclerView interactionImage generation (2)Snapshot NotificationShare snapshot imageDevice location (7)Track device locationBasic pulsing locationLocation camera optionsCustom pulsing locationCustomized location iconShow a user's location on a map fragmentShow a user's location

Rotating camera

activity_camera_slow_spin
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.camera.BoundingBoxCameraActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="50.075"
mapbox:mapbox_cameraTargetLng="14.4302"
mapbox:mapbox_cameraTilt="35"
mapbox:mapbox_cameraZoom="12.5"
mapbox:mapbox_uiCompass="false" />
</FrameLayout>
SlowlyRotatingCameraActivity.java
package com.mapbox.mapboxandroiddemo.examples.camera;
import android.animation.ValueAnimator;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.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();
}
}
Was this page helpful?