Learn how to create and display a scene with a basemap layer and an elevation layer. Set properties of the scene's camera to control the 3D perspective.
Like a map, a scene contains layers of geographic data. It contains a basemap layer and, optionally, one or more data layers. To provide a realistic view of the terrain, you can also add elevation layers to define the height of the surface across the scene. The 3D perspective of the scene is controlled by the scene's camera, which defines the position of the scene observer in 3D space.
In this tutorial, you create and display a scene using the imagery basemap layer. The surface of the scene is defined with an elevation layer and the camera is positioned to display an area of the Santa Monica Mountains in the scene view.
The scene and code will be used as the starting point for other 3D tutorials.
Prerequisites
The following are required for this tutorial:
An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
A scene view is a UI component that displays a scene. It also handles user interactions with the scene, including navigating with touch gestures. Use Xcode and the storyboard editor to add a scene view to the UI and connect it to the view controller source code. Set the scene view size to fill the entire device display.
In the Project Navigator, click ViewController.swift.
In the editor, add an import statement to reference the API and add an @IBOutlet named sceneView and of type AGSSceneView. This will provide a reference to the scene view that you will create in the storyboard.
// Copyright 2020 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 UIKit
import ArcGIS
classViewController: UIViewController{
@IBOutletvar sceneView: AGSSceneView!
privatefuncsetupScene() {
let scene = AGSScene(basemap: .imagery())
let elevationSurface: AGSSurface = {
let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
let surface = AGSSurface()
surface.elevationSources.append(elevationSource)
surface.elevationExaggeration = 2.5return surface
}()
scene.baseSurface = elevationSurface
let initialViewpoint = AGSViewpoint(
center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
scale: 10000,
camera: AGSCamera(
location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
heading: 355.0,
pitch: 72.0,
roll: 0.0 )
)
scene.initialViewpoint = initialViewpoint
sceneView.scene = scene
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupScene()
}
}
In the Project Navigator, click Main.storyboard to open the storyboard editor.
In the menu, click View > Show Library to display the object library.
In the object library browser:
Type uiview or scroll down to find View.
Drag and drop a new view on to the storyboard's main view.
At the bottom right of the storyboard editor, click Add New Constraints. In the panel:
Type 0 for the top, right, bottom, and left constraints.
Click Add 4 Constraints.
The new view expands to fill the display.
In the menu, click View > Inspectors > Show Identity Inspector. In the Inspectors panel, set Custom Class > Class to AGSSceneView.
This sets the type of the new view to AGSSceneView.
In the storyboard editor, right-click on the yellow View Controller icon to display the Connections panel. Drag the sceneView outlet connector to the new AGSSceneViewview on the storyboard.
This connects the AGSSceneView in the storyboard to the sceneView outlet created earlier in the ViewController class.
Add a scene
Use the scene view to display a scene centered on the Santa Monica Mountains in California. The scene will contain an imagery basemap layer.
In Xcode, in the Project Navigator, click ViewController.swift.
In the editor, define a private method named setupScene(). In setupScene() create an AGSScene.
// Copyright 2020 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 UIKit
import ArcGIS
classViewController: UIViewController{
@IBOutletvar sceneView: AGSSceneView!
privatefuncsetupScene() {
let scene = AGSScene(basemap: .imagery())
let elevationSurface: AGSSurface = {
let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
let surface = AGSSurface()
surface.elevationSources.append(elevationSource)
surface.elevationExaggeration = 2.5return surface
}()
scene.baseSurface = elevationSurface
let initialViewpoint = AGSViewpoint(
center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
scale: 10000,
camera: AGSCamera(
location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
heading: 355.0,
pitch: 72.0,
roll: 0.0 )
)
scene.initialViewpoint = initialViewpoint
sceneView.scene = scene
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupScene()
}
}
Create a new AGSSurface and add a new AGSArcGISTiledElevationSource to it to define the base surface for the scene. Set the newly created surface as the base surface of the scene.
An elevation source can define a surface with 3D terrain in a scene. Without an elevation source, the default globe surface is used to display the scene.
// Copyright 2020 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 UIKit
import ArcGIS
classViewController: UIViewController{
@IBOutletvar sceneView: AGSSceneView!
privatefuncsetupScene() {
let scene = AGSScene(basemap: .imagery())
let elevationSurface: AGSSurface = {
let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
let surface = AGSSurface()
surface.elevationSources.append(elevationSource)
surface.elevationExaggeration = 2.5return surface
}()
scene.baseSurface = elevationSurface
let initialViewpoint = AGSViewpoint(
center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
scale: 10000,
camera: AGSCamera(
location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
heading: 355.0,
pitch: 72.0,
roll: 0.0 )
)
scene.initialViewpoint = initialViewpoint
sceneView.scene = scene
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupScene()
}
}
Set the initial viewpoint of the sceneView using an AGSPoint and an AGSCamera.
The position from which you view the scene is defined by an AGSCamera. The following properties of the camera are used to define an observation point in the scene:
// Copyright 2020 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 UIKit
import ArcGIS
classViewController: UIViewController{
@IBOutletvar sceneView: AGSSceneView!
privatefuncsetupScene() {
let scene = AGSScene(basemap: .imagery())
let elevationSurface: AGSSurface = {
let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
let surface = AGSSurface()
surface.elevationSources.append(elevationSource)
surface.elevationExaggeration = 2.5return surface
}()
scene.baseSurface = elevationSurface
let initialViewpoint = AGSViewpoint(
center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
scale: 10000,
camera: AGSCamera(
location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
heading: 355.0,
pitch: 72.0,
roll: 0.0 )
)
scene.initialViewpoint = initialViewpoint
sceneView.scene = scene
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupScene()
}
}
Set the scene property of the sceneView outlet to the new AGSScene.
// Copyright 2020 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 UIKit
import ArcGIS
classViewController: UIViewController{
@IBOutletvar sceneView: AGSSceneView!
privatefuncsetupScene() {
let scene = AGSScene(basemap: .imagery())
let elevationSurface: AGSSurface = {
let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
let surface = AGSSurface()
surface.elevationSources.append(elevationSource)
surface.elevationExaggeration = 2.5return surface
}()
scene.baseSurface = elevationSurface
let initialViewpoint = AGSViewpoint(
center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
scale: 10000,
camera: AGSCamera(
location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
heading: 355.0,
pitch: 72.0,
roll: 0.0 )
)
scene.initialViewpoint = initialViewpoint
sceneView.scene = scene
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupScene()
}
}
In the ViewController's viewDidLoad method, call setupScene() once the view has loaded.
// Copyright 2020 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 UIKit
import ArcGIS
classViewController: UIViewController{
@IBOutletvar sceneView: AGSSceneView!
privatefuncsetupScene() {
let scene = AGSScene(basemap: .imagery())
let elevationSurface: AGSSurface = {
let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
let surface = AGSSurface()
surface.elevationSources.append(elevationSource)
surface.elevationExaggeration = 2.5return surface
}()
scene.baseSurface = elevationSurface
let initialViewpoint = AGSViewpoint(
center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
scale: 10000,
camera: AGSCamera(
location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
heading: 355.0,
pitch: 72.0,
roll: 0.0 )
)
scene.initialViewpoint = initialViewpoint
sceneView.scene = scene
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupScene()
}
}
Set your API key
An API key is required to enable access to services, web maps, and web scenes hosted in ArcGIS Online.
If you haven't already, go to your developer dashboard to get your API key.
For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In the Project Navigator, click AppDelegate.swift.
In the editor, add an import statement to reference the API and in the AppDelegate's application(_:didFinishLaunchingWithOptions:) method, set the apiKey property on the AGSArcGISRuntimeEnvironment with your API Key.
// Copyright 2020 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 UIKit
import ArcGIS
@main
classAppDelegate: UIResponder, UIApplicationDelegate{
funcapplication(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Note: it is not best practice to store API keys in source code.// The API key is referenced here for the convenience of this tutorial.AGSArcGISRuntimeEnvironment.apiKey = "YOUR_API_KEY"returntrue }
// MARK: UISceneSession Lifecyclefuncapplication(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.// Use this method to select a configuration to create the new scene with.returnUISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}
Press <Command+R> to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a scene with the imagery basemap layer centered on the Santa Monica Mountains in California. Drag, pinch, and rotate on the scene view to explore the scene.