Learn how to create and display a map with a basemap layer.
A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. You can display a specific area of a map by using a map view and setting the location and zoom level.
In this tutorial, you create and display a map of the Santa Monica Mountains in California using the topographic basemap layer.
The map and code will be used as the starting point for other 2D 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 map view is a UI component that displays a map. It also handles user interactions with the map, including navigating with touch gestures. Use Xcode and the storyboard editor to add a map view to the UI and connect it to the view controller source code. Set the map view size to fill the entire iPhone display.
In the Project Navigator, click ViewController.swift.
In the editor, add an import statement to reference the API and add an @IBOutlet named mapView and of type AGSMapView. This will provide a reference to the map view that you will create in the storyboard.
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
// 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{
@IBOutletweakvar mapView: AGSMapView!
privatefuncsetupMap() {
let map =AGSMap(
basemapStyle: .arcGISTopographic
)
mapView.map = map
mapView.setViewpoint(
AGSViewpoint(
latitude: 34.02700,
longitude: -118.80500,
scale: 72_000 )
)
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupMap()
}
}
In the storyboard editor, right click on the yellow View Controller icon to display the Connections panel. Drag the mapView outlet connector to the new AGSMapViewview on the storyboard.
This connects the AGSMapView in the storyboard to the mapView outlet created earlier in the ViewController class.
Add a map
Use the map view to display a map centered on the Santa Monica Mountains in California. The map will contain a topographic basemap layer.
In Xcode, in the Project Navigator, click ViewController.swift.
In the editor, define a private method named setupMap(). In setupMap() create an AGSMap.
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
// 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{
@IBOutletweakvar mapView: AGSMapView!
privatefuncsetupMap() {
let map =AGSMap(
basemapStyle: .arcGISTopographic
)
mapView.map = map
mapView.setViewpoint(
AGSViewpoint(
latitude: 34.02700,
longitude: -118.80500,
scale: 72_000 )
)
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupMap()
}
}
Set the map property of the mapView outlet to the new AGSMap.
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
// 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{
@IBOutletweakvar mapView: AGSMapView!
privatefuncsetupMap() {
let map =AGSMap(
basemapStyle: .arcGISTopographic
)
mapView.map = map
mapView.setViewpoint(
AGSViewpoint(
latitude: 34.02700,
longitude: -118.80500,
scale: 72_000 )
)
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupMap()
}
}
In the ViewController's viewDidLoad method, call setupMap() once the view has loaded.
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
// 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{
@IBOutletweakvar mapView: AGSMapView!
privatefuncsetupMap() {
let map =AGSMap(
basemapStyle: .arcGISTopographic
)
mapView.map = map
mapView.setViewpoint(
AGSViewpoint(
latitude: 34.02700,
longitude: -118.80500,
scale: 72_000 )
)
}
overridefuncviewDidLoad() {
super.viewDidLoad()
setupMap()
}
}
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.
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
// Copyright 2021 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
@UIApplicationMainclassAppDelegate: UIResponder, UIApplicationDelegate{
var window: UIWindow?
funcapplication(_application: UIApplication,
didFinishLaunchingWithOptionslaunchOptions: [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 }
}
Press <Command+R> to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a map with the topographic basemap layer centered on the Santa Monica Mountains in California. Pinch, drag, and double-tap the map view to explore the map.