Choose camera controller

View inJavaKotlinView on GitHubSample viewer app

Control the behavior of the camera in a scene.

Image of choose 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 camera around plane" option (i.e. camera will now be fixed to the plane). Choose the "Orbit camera around location" option to rotate and centre the scene around the location of the Upheaval Dome crater structure, or choose the "Free pan round 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 with sceneView.cameraController = CameraController.

Relevant API

  • ArcGISScene
  • Camera
  • GlobeCameraController
  • OrbitGeoElementCameraController
  • OrbitLocationCameraController
  • SceneView

Tags

3D, camera, camera controller

Sample Code

MainActivity.kt
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
/*
 * Copyright 2019 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.arcgisruntime.sample.choosecameracontroller

import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.SpatialReferences
import com.esri.arcgisruntime.mapping.*
import com.esri.arcgisruntime.mapping.view.Camera
import com.esri.arcgisruntime.mapping.view.GlobeCameraController
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.LayerSceneProperties
import com.esri.arcgisruntime.mapping.view.OrbitGeoElementCameraController
import com.esri.arcgisruntime.mapping.view.OrbitLocationCameraController
import com.esri.arcgisruntime.mapping.view.SceneView
import com.esri.arcgisruntime.sample.choosecameracontroller.databinding.ActivityMainBinding
import com.esri.arcgisruntime.symbology.ModelSceneSymbol
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream

class MainActivity : AppCompatActivity() {

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val sceneView: SceneView by lazy {
        activityMainBinding.sceneView
    }

    private val toolbar: androidx.appcompat.widget.Toolbar by lazy {
        activityMainBinding.toolbar
    }

    private lateinit var sceneOverlay: GraphicsOverlay
    private lateinit var plane3d: Graphic
    private lateinit var orbitLocationCameraController: OrbitLocationCameraController
    private lateinit var orbitPlaneCameraController: OrbitGeoElementCameraController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

        // authentication with an API key or named user is required to access basemaps and other
        // location services
        ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)

        // load plane model and texture from assets into cache directory
        copyFilesFromAssetsToCache(resources.getStringArray(R.array.required_files_array))

        // setup Toolbar
        setSupportActionBar(toolbar)
        toolbar.overflowIcon = ContextCompat.getDrawable(this, R.drawable.ic_photo_camera)

        // create a scene and add it to the scene view
        sceneView.scene = ArcGISScene(BasemapStyle.ARCGIS_IMAGERY)

        // add base surface for elevation data
        with(Surface()) {
            this.elevationSources.add(
                ArcGISTiledElevationSource(
                    getString(R.string.world_elevation_service_url)
                )
            )
            sceneView.scene.baseSurface = this
        }

        // create a graphics overlay for the scene
        sceneOverlay = GraphicsOverlay()
        sceneOverlay.sceneProperties.surfacePlacement =
            LayerSceneProperties.SurfacePlacement.ABSOLUTE
        sceneView.graphicsOverlays.add(sceneOverlay)

        // create a camera and set it as the viewpoint for when the scene loads
        val camera = Camera(38.459291, -109.937576, 5500.0, 150.0, 20.0, 0.0)
        sceneView.setViewpointCamera(camera)

        // instantiate a new camera controller which orbits a target location
        with(Point(-109.929589, 38.437304, 1700.0, SpatialReferences.getWgs84())) {
            orbitLocationCameraController = OrbitLocationCameraController(this, 5000.0).apply {
                this.cameraPitchOffset = 3.0
                this.cameraHeadingOffset = 150.0
            }
        }

        loadModel().addDoneLoadingListener {
            // instantiate a new camera controller which orbits the plane at a set distance
            orbitPlaneCameraController = OrbitGeoElementCameraController(plane3d, 100.0).apply {
                this.cameraPitchOffset = 30.0
                this.cameraHeadingOffset = 150.0
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.camera_controller_menu, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        sceneView.cameraController = when (item.itemId) {
            R.id.action_camera_controller_plane -> orbitPlaneCameraController
            R.id.action_camera_controller_crater -> orbitLocationCameraController
            R.id.action_camera_controller_globe -> GlobeCameraController()
            else -> return super.onOptionsItemSelected(item)
        }
        return true
    }

    /**
     * Load the plane model from the cache, use to construct a Model Scene Symbol and add it to the scene's graphic overlay.
     */
    private fun loadModel(): ModelSceneSymbol {
        // create a graphic with a ModelSceneSymbol of a plane to add to the scene
        val pathToModel =
            cacheDir.toString() + File.separator + getString(R.string.file_bristol_model)
        val plane3DSymbol = ModelSceneSymbol(pathToModel, 1.0)
        plane3DSymbol.heading = 45.0
        plane3d =
            Graphic(
                Point(-109.937516, 38.456714, 5000.0, SpatialReferences.getWgs84()),
                plane3DSymbol
            )
        sceneOverlay.graphics.add(plane3d)
        return plane3DSymbol
    }

    /**
     * Copy the given file from the app's assets folder to the app's cache directory.
     *
     * @param files as String
     */
    private fun copyFilesFromAssetsToCache(files: Array<String>) {
        applicationContext.assets?.let { assetManager ->
            files.forEach { filename ->
                with(File(cacheDir.toString() + File.separator + filename)) {
                    if (!this.exists()) {
                        try {
                            val bis = BufferedInputStream(assetManager.open(filename))
                            val bos = BufferedOutputStream(
                                FileOutputStream(cacheDir.toString() + File.separator + filename)
                            )
                            val buffer = ByteArray(bis.available())
                            var read = bis.read(buffer)
                            while (read != -1) {
                                bos.write(buffer, 0, read)
                                read = bis.read(buffer)
                            }
                            bos.close()
                            bis.close()
                            Log.i(logTag, "$filename copied to cache.")
                        } catch (e: Exception) {
                            logToUser(
                                getString(
                                    R.string.error_writing_to_cache,
                                    filename,
                                    e.message
                                )
                            )
                        }
                    } else {
                        Log.i(logTag, "$files already in cache.")
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        sceneView.resume()
    }

    override fun onPause() {
        sceneView.pause()
        super.onPause()
    }

    override fun onDestroy() {
        sceneView.dispose()
        super.onDestroy()
    }

    /**
     * AppCompatActivityExtensions
     */
    private val AppCompatActivity.logTag: String get() = this::class.java.simpleName

    private fun AppCompatActivity.logToUser(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
        Log.d(logTag, message)
    }
}

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