Display a map

Learn how to create and display a map with a basemap layer.

display a map

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:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.
  3. The ArcGIS Runtime API for iOS is installed.

Steps

Create a new Xcode project

Use Xcode to create a single view iOS app and configure it to reference the API.

  1. Open Xcode. In the menu bar, click File > New > Project > iOS > Single View App > Next.

    • In the Choose options window, set the following properties:
      • Product Name: <your app name>
      • Language: Swift
      • User interface: Storyboard
      • Organization Identifier: <your organization>
    • Uncheck all other options.
    • Click Next > Create.
  2. Add a reference to the API by following the instructions in Get the API - Configure a project.

Add a map view to the UI

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.

  1. In the Project Navigator, click ViewController.swift.

  2. 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.

    ViewController.swift
    Add line.Add line.
    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 17 18 19 20 21 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 25 26 26 26 27 28 29
    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
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let map = AGSMap(
                basemapStyle: .arcGISTopographic
            )
    
            mapView.map = map
    
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.02700,
                    longitude: -118.80500,
                    scale: 72_000
                )
            )
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    

    AGSMapView is a subclass of UIView.

  3. In the Project Navigator, click Main.storyboard to open the storyboard editor.

  4. In the menu, click View > Show Library to display the object library.

  5. 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.
  6. 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.

  7. In the menu, click View > Inspectors > Show Identity Inspector. In the Inspectors panel, set Custom Class > Class to AGSMapView.

    This sets the type of the new view to AGSMapView.

  8. 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 AGSMapView view 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.

  1. In Xcode, in the Project Navigator, click ViewController.swift.

  2. In the editor, define a private method named setupMap(). In setupMap() create an AGSMap.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 24 25 26 27 28 29 29 29 30 31 32 33 34 35 36 37 38 39 40 41 42 42 42 43 44 45
    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
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let map = AGSMap(
                basemapStyle: .arcGISTopographic
            )
    
            mapView.map = map
    
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.02700,
                    longitude: -118.80500,
                    scale: 72_000
                )
            )
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    
  3. Set the map property of the mapView outlet to the new AGSMap.

    ViewController.swift
    Add line.
    23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 40 40 40 40 39 38 38 38 38
    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
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let map = AGSMap(
                basemapStyle: .arcGISTopographic
            )
    
            mapView.map = map
    
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.02700,
                    longitude: -118.80500,
                    scale: 72_000
                )
            )
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    
  4. In the ViewController's viewDidLoad method, call setupMap() once the view has loaded.

    ViewController.swift
    Add line.
    41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 43 44 45 46 47 47 47
    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
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let map = AGSMap(
                basemapStyle: .arcGISTopographic
            )
    
            mapView.map = map
    
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.02700,
                    longitude: -118.80500,
                    scale: 72_000
                )
            )
    
        }
    
        override func viewDidLoad() {
            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.

  1. In the Project Navigator, click AppDelegate.swift.

  2. 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.

    AppDelegate.swift
    Add line.Add line.
    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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
    
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
    
        func application(_ 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"
    
            return true
        }
    
    }
    
  3. 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 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.

What's next?

Learn how to use additional API features, ArcGIS platform services, and ArcGIS tools in these tutorials:

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