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
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
Steps
Create a new Xcode project
Use Xcode to create an iOS app and configure it to reference the API.
-
Open Xcode. In the menu bar, click File > New > Project > iOS > App > Next.
- In the Choose options window, set the following properties:
- Product Name:
<your app name
> - Language: Swift
- Interface: SwiftUI
- Organization Identifier:
<your organization
>
- Product Name:
- Uncheck all other options.
- Click Next.
- Choose a location to store your project. Click Create.
- In the Choose options window, set the following properties:
-
In the Project Navigator, click
<your app name
. In the Editor, right click on the struct name,>App <your app name
. Select Refactor then Rename... to rename the struct to>App Main
. Click the Rename button in the top right to confirm the new name. This will rename the struct and file in all affected areas. This file and struct will be namedApp Main
for all tutorials here on out.App -
Add a reference to the API using Swift Package Manager.
Get an access token
You need an access token to use the location services used in this tutorial.
-
Go to the Create an API key tutorial to obtain an access token.
-
Ensure that the following privilege is enabled: Location services > Basemaps > Basemap styles service.
-
Copy the access token as it will be used in the next step.
To learn more about other ways to get an access token, go to Types of authentication.
Set your API key
-
In the Project Navigator, click MainApp.swift.
-
In the Editor, add an
import
statement to reference the API.MainApp.swiftUse dark colors for code blocks 16 17 19Add line. import SwiftUI import ArcGIS
-
Implement an initializer in the
Main
struct. Set theApp ArcGISEnvironment.apiKey
property with your access token.MainApp.swiftUse dark colors for code blocks 16 17 18 19 20 21 22 26 27 28 29 30 31 32 33 34 35 36Add line. Add line. Add line. import SwiftUI import ArcGIS @main struct MainApp: App { init() { ArcGISEnvironment.apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>") } var body: some SwiftUI.Scene { WindowGroup { ContentView() .ignoresSafeArea() } } }
Add a map
Create a map that contains a topographic basemap layer. The map will be centered on the Santa Monica Mountains in California.
-
In the Project Navigator, click ContentView.swift.
-
In the Editor, add an
import
statement to reference the API.ContentView.swiftUse dark colors for code blocks 16 17 19Add line. import SwiftUI import ArcGIS
-
Add a
@
property wrapper namedState map
of typeMap
with a default value. Create aMap
with anarc
basemap style and return it.GIS Topographic ContentView.swiftUse dark colors for code blocks 20 21 27 28Add line. Add line. Add line. Add line. Add line. struct ContentView: View { @State private var map = { let map = Map(basemapStyle: .arcGISTopographic) return map }() }
A
GeoModel
like aMap
orScene
can be expensive to create and holds state. To ensure that a geo model and other model objects are only created as needed, you can apply the@
orState @
property wrapper to them, as in the above code. See Managing model data in your app for more information.State Object -
Set the map's
initial
property with aViewpoint Viewpoint
using the Santa Monica Mountains coordinates.ContentView.swiftUse dark colors for code blocks 20 21 22 23 24 26 27 28 29 30Add line. struct ContentView: View { @State private var map = { let map = Map(basemapStyle: .arcGISTopographic) map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000) return map }() }
Add a map view
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 SwiftUI to add a map view.
-
In the body, create a
MapView
with the map created in the previous step.ContentView.swiftUse dark colors for code blocks 20 21 22 23 24 25 26 27 28 29 30 31 34 35 36 37Add line. Add line. struct ContentView: View { @State private var map = { let map = Map(basemapStyle: .arcGISTopographic) map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000) return map }() var body: some View { // Displays the map. MapView(map: map) } }
-
In the Project Navigator, click MainApp.swift.
-
In the body, add an
.ignores
modifier to theSafe Area() Content
. TheView Content
's body contains theView Map
and this modifier ensures that the map view is displayed beyond the safe area to all edges.View MainApp.swiftUse dark colors for code blocks 27 28 29 30 32 33 34Add line. var body: some SwiftUI.Scene { WindowGroup { ContentView() .ignoresSafeArea() } }
-
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Monterey 12.5, Xcode 15, iOS 17. 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 location services, and ArcGIS tools in these tutorials: