Skip to content

Change camera controller

View on GitHub

Control the behavior of the camera in a scene.

Image of change camera controller

Use case

The globe camera controller (the default camera controller in all new scenes) allows a user to explore the scene freely by zooming in/out and panning around the globe. The orbit camera controllers fix the camera to look at a target location or geoelement. A primary use case is for following moving objects like cars and planes.

How to use the sample

The application loads with the default globe camera controller. To rotate and fix the scene around the plane, exit globe mode by choosing the "Orbit plane" option (i.e. camera will now be fixed to the plane). Choose the "Orbit crater" option to rotate and center the scene around the location of the Upheaval Dome crater structure, or choose the "Globe" option to return to default free navigation.

How it works

  1. Create an instance of a class extending CameraController: GlobeCameraController, OrbitLocationCameraController, OrbitGeoElementCameraController.
  2. Set the scene view's camera controller by setting the ArcGISSceneViewController.cameraController property.

Relevant API

  • ArcGISScene
  • ArcGISSceneViewController
  • Camera
  • GlobeCameraController
  • OrbitGeoElementCameraController
  • OrbitLocationCameraController

Tags

3D, camera controller, orbit, tracking

Sample Code

change_camera_controller.dart
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2025 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
//
//   https://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.
//

import 'dart:io';

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

class ChangeCameraController extends StatefulWidget {
  const ChangeCameraController({super.key});

  @override
  State<ChangeCameraController> createState() => _ChangeCameraControllerState();
}

class _ChangeCameraControllerState extends State<ChangeCameraController>
    with SampleStateSupport {
  // Create a controller for the scene view.
  final _sceneViewController = ArcGISSceneView.createController();
  // A flag for when the scene view is ready and controls can be used.
  var _ready = false;

  // The graphic for the plane.
  Graphic? _planeGraphic;

  // The type of CameraController currently being used.
  var _selectedCameraControllerKind = CameraControllerKind.globe;

  // List of entries for the dropdown menu.
  final _cameraControllerDropdownEntries =
      <DropdownMenuEntry<CameraControllerKind>>[];

  @override
  void initState() {
    super.initState();

    // Build up the dropdown menu entries.
    _cameraControllerDropdownEntries.addAll([
      const DropdownMenuEntry(
        value: CameraControllerKind.globe,
        label: 'Globe',
      ),
      const DropdownMenuEntry(
        value: CameraControllerKind.orbitLocation,
        label: 'Orbit crater',
      ),
      const DropdownMenuEntry(
        value: CameraControllerKind.orbitGeoElement,
        label: 'Orbit plane',
      ),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        left: false,
        right: false,
        child: Stack(
          children: [
            Column(
              children: [
                Expanded(
                  // Add a scene view to the widget tree and set a controller.
                  child: ArcGISSceneView(
                    controllerProvider: () => _sceneViewController,
                    onSceneViewReady: onSceneViewReady,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Text('Select Camera Controller:'),
                    // Create a dropdown menu to select a feature layer source.
                    DropdownMenu(
                      dropdownMenuEntries: _cameraControllerDropdownEntries,
                      trailingIcon: const Icon(Icons.arrow_drop_down),
                      textAlign: TextAlign.center,
                      textStyle: Theme.of(context).textTheme.labelMedium,
                      hintText: 'Select a camera controller',
                      onSelected: handleCameraControllerSelection,
                      initialSelection: _selectedCameraControllerKind,
                    ),
                  ],
                ),
              ],
            ),
            // Display a progress indicator and prevent interaction until state is ready.
            LoadingIndicator(visible: !_ready),
          ],
        ),
      ),
    );
  }

  Future<void> onSceneViewReady() async {
    // Add the scene to the view controller.
    final scene = _setupScene();
    _sceneViewController.arcGISScene = scene;

    // Graphics overlay for the plane
    final graphicsOverlay = GraphicsOverlay();
    _planeGraphic = await _setupPlaneGraphic();
    graphicsOverlay.graphics.add(_planeGraphic!);
    graphicsOverlay.sceneProperties.surfacePlacement =
        SurfacePlacement.absolute;
    _sceneViewController.graphicsOverlays.add(graphicsOverlay);

    // Set the ready state variable to true to enable the sample UI.
    setState(() => _ready = true);
  }

  // Function to handle when the user selects a camera controller from the
  // dropdown menu.
  void handleCameraControllerSelection(
    CameraControllerKind? cameraControllerKind,
  ) {
    if (cameraControllerKind == null || _planeGraphic == null) return;

    // Set the appropriate camera controller based on the user's selection.
    switch (cameraControllerKind) {
      case CameraControllerKind.globe:
        _sceneViewController.cameraController = GlobeCameraController();
      case CameraControllerKind.orbitGeoElement:
        final cameraController = OrbitGeoElementCameraController(
          targetGeoElement: _planeGraphic!,
          distance: 1700,
        );

        cameraController.cameraPitchOffset = 3;
        cameraController.cameraHeadingOffset = 150;
        _sceneViewController.cameraController = cameraController;
      case CameraControllerKind.orbitLocation:
        final cameraController =
            OrbitLocationCameraController.withTargetPositionAndCameraDistance(
              targetLocation: ArcGISPoint(
                x: -109.929589,
                y: 38.437304,
                z: 1700,
                spatialReference: SpatialReference.wgs84,
              ),
              distance: 10000,
            );

        cameraController.cameraPitchOffset = 3;
        cameraController.cameraHeadingOffset = 150;
        _sceneViewController.cameraController = cameraController;
    }

    setState(() {
      _selectedCameraControllerKind = cameraControllerKind;
    });
  }

  ArcGISScene _setupScene() {
    // Create a scene with an imagery basemap style.
    final scene = ArcGISScene.withBasemapStyle(BasemapStyle.arcGISImagery);

    // Set the scene's initial viewpoint.
    scene.initialViewpoint = Viewpoint.withPointScaleCamera(
      center: ArcGISPoint(x: 0, y: 0),
      scale: 1,
      camera: Camera.withLookAtPoint(
        lookAtPoint: ArcGISPoint(
          x: -109.92845172437961,
          y: 38.440455578104029,
          spatialReference: SpatialReference.wgs84,
        ),
        distance: 10000,
        heading: 150,
        pitch: 20,
        roll: 0,
      ),
    );

    // Add surface elevation to the scene.
    final worldElevationService = Uri.parse(
      'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer',
    );
    final elevationSource = ArcGISTiledElevationSource.withUri(
      worldElevationService,
    );
    scene.baseSurface.elevationSources.add(elevationSource);

    return scene;
  }

  Future<Graphic> _setupPlaneGraphic() async {
    const downloadFileName = 'Bristol';
    final appDir = await getApplicationDocumentsDirectory();
    final zipFile = File('${appDir.absolute.path}/$downloadFileName.zip');
    final bristolFile = File(
      '${appDir.absolute.path}/$downloadFileName/$downloadFileName.dae',
    );
    // Download the plane model files.
    if (!zipFile.existsSync()) {
      await downloadSampleDataWithProgress(
        itemIds: ['681d6f7694644709a7c830ec57a2d72b'],
        destinationFiles: [zipFile],
      );
    }

    // Define the plane's position.
    final planePosition = ArcGISPoint(
      x: -109.937516,
      y: 38.456714,
      z: 5000,
      spatialReference: SpatialReference.wgs84,
    );

    // Define the plane symbol.
    final planeSymbol = ModelSceneSymbol.withUri(
      uri: Uri.parse(bristolFile.path),
      scale: 50,
    );

    // Create the plane's graphic with the location and symbol.
    final planeGraphic = Graphic(geometry: planePosition, symbol: planeSymbol);

    return planeGraphic;
  }
}

// Enumeration listing the kinds of camera controllers available in this sample.
enum CameraControllerKind { globe, orbitLocation, orbitGeoElement }

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.