Fix the camera to point at and rotate around a target object.

Use case
The orbit geoelement camera controller provides control over the following camera behaviors:
- Automatically track the target
- Stay near the target by setting a minimum and maximum distance offset
- Restrict where you can rotate around the target
- Automatically rotate the camera when the target’s heading and pitch changes
- Disable user interactions for rotating the camera
- Animate camera movement over a specified duration
- Control the vertical positioning of the target on the screen
- Set a target offset (e.g., to orbit around the tail of the plane) instead of defaulting to orbiting the center of the object
How to use the sample
The sample loads with the camera orbiting an airplane model. The camera is preset with a restricted camera heading, pitch, and a limited minimum and maximum camera distance set from the plane. The position of the plane on the screen is also set just below center.
Tap “Cockpit” to offset and fix the camera into the cockpit of the airplane. The camera will follow the pitch of the plane in this mode. In this view, adjusting the camera distance is disabled. Tap “Center” to exit the cockpit view and fix the camera controller on the center of the plane.
Use the “Camera Heading” slider to adjust the camera heading. Use the “Plane Pitch” slider to adjust the plane’s pitch. When not in Cockpit view, the plane’s pitch will change independently to that of the camera pitch.
Toggle on the “Allow Camera Distance Interaction” switch to allow zooming in and out by pinching. When the toggle is off, the user will be unable to adjust the camera distance.
How it works
- Instantiate an
orbitCameraControllerwith aGeoElementand camera distance as parameters. - Set the orbitCameraController to the scene view.
- Set the
setCameraPitchOffset,setCameraHeadingOffset, anddistanceproperties for the orbitCameraController. - Set the minimum and maximum angle of heading and pitch, and minimum and maximum distance for the camera.
- Set the distance from which the camera is offset from the plane using
setTargetOffsets(x:y:z:duration:)or the properties. - use isAutoPitchEnabled to make camera follow plane pitch in cockpit view
- use isCameraDistanceInteractive to enable/disable finger interactions
- Animate the camera to the cockpit using
moveToCockpitView().
Relevant API
- OrbitGeoElementCameraController
Offline data
This sample uses scene data. It is downloaded from ArcGIS Online automatically.
Tags
3D, camera, object, orbit, rotate, scene
Sample code
/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.orbitcameraaroundobject.components
import android.app.Applicationimport androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.arcgismaps.geometry.Pointimport com.arcgismaps.geometry.SpatialReferenceimport com.arcgismaps.mapping.ArcGISSceneimport com.arcgismaps.mapping.ArcGISTiledElevationSourceimport com.arcgismaps.mapping.BasemapStyleimport com.arcgismaps.mapping.Viewpointimport com.arcgismaps.mapping.symbology.DistanceCompositeSceneSymbolimport com.arcgismaps.mapping.symbology.DistanceSymbolRangeimport com.arcgismaps.mapping.symbology.ModelSceneSymbolimport com.arcgismaps.mapping.symbology.SimpleRendererimport com.arcgismaps.mapping.view.Cameraimport com.arcgismaps.mapping.view.Graphicimport com.arcgismaps.mapping.view.GraphicsOverlayimport com.arcgismaps.mapping.view.OrbitGeoElementCameraControllerimport com.arcgismaps.mapping.view.SurfacePlacementimport com.arcgismaps.toolkit.geoviewcompose.SceneViewProxyimport com.esri.arcgismaps.sample.orbitcameraaroundobject.Rimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModelimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.asStateFlowimport kotlinx.coroutines.flow.updateimport kotlinx.coroutines.launchimport java.io.File
class OrbitCameraAroundObjectViewModel(app: Application) : AndroidViewModel(app) {
// Lazy provision path for reference offline resources. private val provisionPath: String by lazy { app.getExternalFilesDir(null)?.path.toString() + File.separator + app.getString(R.string.orbit_camera_around_object_app_name) }
// Construct the model file URI from the provision path. private val bristolModelUri get() = "$provisionPath${File.separator}Bristol.dae"
// The model (3D) graphic target. private val planePosition = Point( x = -2.708, y = 56.096, z = 5000.0, spatialReference = SpatialReference.wgs84() )
// The plane's own 3D model symbol. private val planeModelSymbol: ModelSceneSymbol by lazy { ModelSceneSymbol( uri = bristolModelUri, scale = 100.0F ) }
// Distance composite symbol showing the detailed 3D model at close range. private val distanceCompositeSymbol: DistanceCompositeSceneSymbol by lazy { DistanceCompositeSceneSymbol().apply { ranges.add( DistanceSymbolRange( symbol = planeModelSymbol, minDistance = null, maxDistance = null ) ) } }
// Graphic for the plane using the distance composite symbol. // Define PITCH attributes for OrbitController's AutoPitch function private val planeGraphic by lazy { Graphic( geometry = planePosition, attributes = mapOf("PITCH" to 0.0), symbol = distanceCompositeSymbol ) }
// Orbit camera controller that targets the plane graphic. val orbitCameraController: OrbitGeoElementCameraController by lazy { OrbitGeoElementCameraController(planeGraphic, 2000.0).apply { setCameraPitchOffset(50.0) setCameraHeadingOffset(0.0) } }
// Graphics overlay to display the plane graphic using a distance composite symbol. private val graphicsOverlay by lazy { GraphicsOverlay(graphics = listOf(planeGraphic)).apply { sceneProperties.surfacePlacement = SurfacePlacement.Relative // Rotate the plane graphic based on its PITCH attribute. renderer = SimpleRenderer().apply { sceneProperties.pitchExpression = "[PITCH]" } } }
// Expose the graphics overlay list for the SceneView val graphicsOverlays = listOf(graphicsOverlay)
// SceneView proxy to hand to the composable SceneView val sceneViewProxy = SceneViewProxy()
val arcGISScene = ArcGISScene(BasemapStyle.ArcGISImagery).apply { // Set the tiled elevation source baseSurface.elevationSources += ArcGISTiledElevationSource( uri = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer" ) // Set the initial viewpoint val camera = Camera( latitude = 56.096, longitude = -2.708, altitude = 5000.0, heading = -30.0, pitch = 80.0, roll = 0.0 ) initialViewpoint = Viewpoint(boundingGeometry = camera.location, camera = camera) }
// Create a state flow to hold the UI state for the supporting pane controls private val _orbitCameraUiState = MutableStateFlow(OrbitCameraUiState.defaultState)
// Expose the state flow as read-only for the UI val orbitCameraUiState = _orbitCameraUiState.asStateFlow()
// Create a message dialog view model for handling error messages val messageDialogVM = MessageDialogViewModel()
init { // Load the Scene and handle any errors by showing a message dialog viewModelScope.launch { arcGISScene.load().onFailure { messageDialogVM.showMessageDialog(it) } }
// Load the plane's 3D model asset up front too. This is a separate, independent load // from the scene above (it reads Bristol.dae off disk), and switchViewMode() awaits it // before moving the camera so a cockpit-view tap before the model has loaded doesn't // lock the camera onto a not-yet-known model bounds. viewModelScope.launch { planeModelSymbol.load().onFailure { messageDialogVM.showMessageDialog(it) } }
// Restrict the camera's heading/pitch to stay behind the plane. orbitCameraController.minCameraHeadingOffset = -180.0 orbitCameraController.maxCameraHeadingOffset = 180.0
orbitCameraController.minCameraPitchOffset = -90.0 orbitCameraController.maxCameraPitchOffset = 90.0
// Restrict the camera to stay within 100 meters of the plane. orbitCameraController.maxCameraDistance = 10000.0
// Position the plane a third from the top of the screen, // so it isn't covered by the settings sheet. orbitCameraController.targetVerticalScreenFactor = 0.66F
// Don't pitch the camera when the plane pitches. orbitCameraController.isAutoPitchEnabled = false
}
// Adjust Camera Heading fun setCameraHeading(sliderValue: Float) { orbitCameraController.setCameraHeadingOffset(sliderValue.toDouble()) _orbitCameraUiState.update { it.copy(heading = sliderValue) } }
// Adjust Plane Pitch fun setPlanePitch(sliderValue: Float) { // Update the plane's PITCH attribute, which rotates the graphic (via the renderer's // pitch expression) and, when isAutoPitchEnabled is true, the orbit camera along with it. planeGraphic.attributes["PITCH"] = sliderValue.toDouble() _orbitCameraUiState.update { it.copy(pitch = sliderValue) } }
// Switch View Mode fun switchViewMode(viewMode: ViewMode) { _orbitCameraUiState.update { it.copy(viewMode = viewMode) } viewModelScope.launch { // Wait for both scene and Symbol to load before change view arcGISScene.load().onFailure { messageDialogVM.showMessageDialog(it) return@launch } planeModelSymbol.load().onFailure { messageDialogVM.showMessageDialog(it) return@launch } when (viewMode) { ViewMode.Cockpit -> moveToCockpitView() ViewMode.Center -> moveToCenterView() } } }
/** * Animates the camera into a first-person cockpit view inside the plane. * * Locks [orbitCameraController]'s pitch to look straight ahead, disables distance * interaction so the camera stays anchored at the cockpit position, and moves the target * offset to the cockpit location before zooming the camera in, so the pivot is never wrong * while the camera is close to the plane. */ private suspend fun moveToCockpitView() { orbitCameraController.apply { isCameraDistanceInteractive = false isAutoPitchEnabled = true // Move the pivot to the cockpit location first, while the camera is still far away, // so the shift is a subtle pan rather than a jarring jump once the camera is close. setTargetOffsets(x = 0.0, y = -200.0, z = 110.0, duration = 1.0F) .onFailure { messageDialogVM.showMessageDialog(it) } // Now that the pivot is in place, zoom the camera in to the cockpit. moveCamera( distanceDelta = 1.0 - cameraDistance.value, headingDelta = 0.0 - cameraHeadingOffset.value, pitchDelta = 90.0 - cameraPitchOffset.value, duration = 1.0F ).onFailure { messageDialogVM.showMessageDialog(it) } // Narrow the distance/pitch bounds only after the camera has animated into place, so // the animation isn't cut short by an instant clamp to the cockpit's tight range. minCameraDistance = 0.0 maxCameraDistance = 10.0 minCameraPitchOffset = 90.0 maxCameraPitchOffset = 90.0 } _orbitCameraUiState.update { it.copy(heading = 0F, allowCameraDistanceInteraction = false) } }
/** * Animates the camera into an orbiting view centered on the plane. * * Restores [orbitCameraController]'s full pitch range and maximum distance, sets distance * interaction according to the current [OrbitCameraUiState.allowCameraDistanceInteraction] * setting, and pulls the camera back before recentering the target offset, so the pivot * shift happens while the camera is already far from the plane. */ private suspend fun moveToCenterView() { orbitCameraController.apply { minCameraPitchOffset = -90.0 maxCameraPitchOffset = 90.0 maxCameraDistance = 10000.0 minCameraDistance = 0.0 isAutoPitchEnabled = false isCameraDistanceInteractive = _orbitCameraUiState.value.allowCameraDistanceInteraction // Pull the camera back first, while it's still anchored on the cockpit pivot. moveCamera( distanceDelta = 1000.0 - cameraDistance.value, headingDelta = 0.0 - cameraHeadingOffset.value, pitchDelta = 0.0, duration = 1.0F ).onFailure { messageDialogVM.showMessageDialog(it) } // Now that the camera is far away, recenter the pivot — a subtle pan at this distance. setTargetOffsets(x = 0.0, y = 0.0, z = 20.0, duration = 0.5F) .onFailure { messageDialogVM.showMessageDialog(it) } } _orbitCameraUiState.update { it.copy(heading = 0F) } }
// Toggle Interaction mode fun setInteraction(value: Boolean) { orbitCameraController.isCameraDistanceInteractive = value _orbitCameraUiState.update { it.copy(allowCameraDistanceInteraction = value) } }}
// Data Class for Current UI statedata class OrbitCameraUiState( val heading: Float, val pitch: Float, val viewMode: ViewMode, val allowCameraDistanceInteraction: Boolean) { companion object { val defaultState = OrbitCameraUiState( heading = 0F, pitch = 0F, viewMode = ViewMode.Center, allowCameraDistanceInteraction = true )
}}
enum class ViewMode { Cockpit, Center}/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.orbitcameraaroundobject
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport com.esri.arcgismaps.sample.orbitcameraaroundobject.screens.OrbitCameraAroundObjectScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { SampleAppTheme { Surface(color = MaterialTheme.colorScheme.background) { OrbitCameraAroundObjectScreen() } } } }}/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.orbitcameraaroundobject.screens
import androidx.compose.foundation.layout.BoxScopeimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Scaffoldimport androidx.compose.runtime.Composableimport androidx.compose.ui.Modifierimport androidx.compose.ui.res.stringResourceimport androidx.lifecycle.compose.collectAsStateWithLifecycleimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.toolkit.geoviewcompose.SceneViewimport com.esri.arcgismaps.sample.orbitcameraaroundobject.Rimport com.esri.arcgismaps.sample.orbitcameraaroundobject.components.OrbitCameraUiStateimport com.esri.arcgismaps.sample.orbitcameraaroundobject.components.OrbitCameraAroundObjectViewModelimport com.esri.arcgismaps.sample.orbitcameraaroundobject.components.ViewModeimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogimport com.esri.arcgismaps.sample.sampleslib.components.SampleDeviceLightDarkPreviewimport com.esri.arcgismaps.sample.sampleslib.components.SamplePreviewSurfaceimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBarimport com.esri.arcgismaps.sample.sampleslib.components.adaptive.AdaptiveThreePane
/** * Main composable screen for the sample. * It owns the ViewModel and hoists UI state for the scaffold and the MapView. */@Composablefun OrbitCameraAroundObjectScreen( viewModel: OrbitCameraAroundObjectViewModel = viewModel()) { val orbitCameraUiState = viewModel.orbitCameraUiState.collectAsStateWithLifecycle().value
MainScreenScaffold( orbitCameraUiState = orbitCameraUiState, setCameraHeading = viewModel::setCameraHeading, setCameraPitch = viewModel::setPlanePitch, switchViewMode = viewModel::switchViewMode, setInteraction = viewModel::setInteraction, mainPaneContent = { SceneView( arcGISScene = viewModel.arcGISScene, sceneViewProxy = viewModel.sceneViewProxy, cameraController = viewModel.orbitCameraController, graphicsOverlays = viewModel.graphicsOverlays ) } )
viewModel.messageDialogVM.apply { if (dialogStatus) { MessageDialog( title = messageTitle, description = messageDescription, onDismissRequest = ::dismissDialog ) } }}
@Composableprivate fun MainScreenScaffold( orbitCameraUiState: OrbitCameraUiState, setCameraHeading: (Float) -> Unit = {}, setCameraPitch: (Float) -> Unit = {}, switchViewMode: (ViewMode) -> Unit = {}, setInteraction: (Boolean) -> Unit = {}, mainPaneContent: @Composable BoxScope.() -> Unit) { Scaffold( topBar = { SampleTopAppBar(title = stringResource(R.string.orbit_camera_around_object_app_name)) }, content = { paddingValues -> AdaptiveThreePane( modifier = Modifier .fillMaxSize() .padding(paddingValues), supportingPaneTitle = "Settings", mainPane = { _, _ -> mainPaneContent() }, supportingPane = { _, _ -> OrbitCameraAroundObjectSupportingPane( orbitCameraUiState = orbitCameraUiState, setCameraHeading = setCameraHeading, setCameraPitch = setCameraPitch, cameraMode = switchViewMode, setInteraction = setInteraction ) } ) } )}
@SampleDeviceLightDarkPreview@Composablefun MainScreenPreview() { SamplePreviewSurface { MainScreenScaffold( orbitCameraUiState = OrbitCameraUiState.defaultState, mainPaneContent = {} ) }}/* Copyright 2026 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.orbitcameraaroundobject.screens
import androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.material3.ButtonDefaultsimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.RadioButtonimport androidx.compose.material3.Sliderimport androidx.compose.material3.Surfaceimport androidx.compose.material3.Switchimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.unit.dpimport com.esri.arcgismaps.sample.orbitcameraaroundobject.components.OrbitCameraUiStateimport com.esri.arcgismaps.sample.orbitcameraaroundobject.components.ViewMode
/** * Supporting pane content for the sample. */@Composableinternal fun OrbitCameraAroundObjectSupportingPane( orbitCameraUiState: OrbitCameraUiState, setCameraHeading: (Float) -> Unit, setCameraPitch: (Float) -> Unit, cameraMode: (ViewMode) -> Unit, setInteraction: (Boolean) -> Unit) { ViewMode.entries.forEach { mode -> SelectionRow( title = mode.name, description = when (mode) { ViewMode.Center -> { "Default centered view of the 3D model." }
ViewMode.Cockpit -> { "Cockpit view from the 3D model." } }, selected = orbitCameraUiState.viewMode == mode, onClick = { cameraMode(mode) } ) }
ToggleRow( title = "Allow Camera distance interaction", description = "Enabled when in center mode, disabled when in cockpit mode.", isToggleChecked = orbitCameraUiState.allowCameraDistanceInteraction, isToggleEnabled = orbitCameraUiState.viewMode == ViewMode.Center, onCheckedChange = setInteraction ) HeadingSlider(orbitCameraUiState.heading, setCameraHeading) PitchSlider(orbitCameraUiState.pitch, setCameraPitch)}
@Composablefun CameraSlider( title: String, sliderValue: Float, sliderRangeValue: ClosedFloatingPointRange<Float>, onSliderValueChanged: (Float) -> Unit) { Column(modifier = Modifier.fillMaxWidth()) { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { Text(text = title, style = MaterialTheme.typography.titleMedium) Text( text = sliderValue.toInt().toString(), style = MaterialTheme.typography.titleMedium ) } Slider( modifier = Modifier.fillMaxWidth(), value = sliderValue, onValueChange = onSliderValueChanged, valueRange = sliderRangeValue ) }}
@Composableprivate fun HeadingSlider(heading: Float, onHeadingChanged: (Float) -> Unit) { CameraSlider( title = "Camera Heading", sliderValue = heading, sliderRangeValue = -180f..180f, onSliderValueChanged = onHeadingChanged )}
@Composableprivate fun PitchSlider(pitch: Float, onPitchChanged: (Float) -> Unit) { CameraSlider( title = "Plane Pitch", sliderValue = pitch, sliderRangeValue = -90f..90f, onSliderValueChanged = onPitchChanged )}
@Composableprivate fun ToggleRow( title: String, description: String, isToggleChecked: Boolean, isToggleEnabled: Boolean, onCheckedChange: (Boolean) -> Unit) { Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween ) { Column( modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp) ) { Text( text = title, style = MaterialTheme.typography.titleMedium ) Text( text = description, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant ) } Switch( enabled = isToggleEnabled, checked = isToggleChecked, onCheckedChange = onCheckedChange ) }}
@Composableprivate fun SelectionRow( title: String, description: String, selected: Boolean, onClick: () -> Unit) { Surface( modifier = Modifier.fillMaxWidth(), onClick = onClick, shape = RoundedCornerShape(12.dp), color = if (selected) MaterialTheme.colorScheme.secondaryContainer else MaterialTheme.colorScheme.surface, border = ButtonDefaults.outlinedButtonBorder(enabled = true) ) { Row( modifier = Modifier .fillMaxWidth() .padding(12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically ) { RadioButton( selected = selected, onClick = onClick ) Column { Text( text = title, style = MaterialTheme.typography.bodyLarge ) Text( text = description, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant ) } } }}