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.
Set up authentication
To access the secure ArcGIS location services used in this tutorial, you must implement API key authentication or user authentication using an ArcGIS Location Platform or an ArcGIS Online account.
You can implement API key authentication or user authentication in this tutorial. Compare the differences below:
API key authentication
- Users are not required to sign in.
- Requires creating an API key credential with the correct privileges.
- API keys are long-lived access tokens.
- Service usage is billed to the API key owner/developer.
- Simplest authentication method to implement.
- Recommended approach for new ArcGIS developers.
Learn more in API key authentication.
User authentication
- Users are required to sign in with an ArcGIS account.
- User accounts must have privilege to access the ArcGIS services used in application.
- Requires creating OAuth credentials.
- Application uses a redirect URL and client ID.
- Service usage is billed to the organization of the user signed into the application.
Learn more in User authentication.
Create a new API key access token with privileges to access the secure resources used in this tutorial.
-
Complete the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
-
Copy and paste the API Key access token into a safe location. It will be used in a later step.
Develop or Download
To complete this tutorial you have 2 options:
Option 1: Develop the code
Create a new app
To get started, use Xcode to create an iOS app and configure it to reference the API.
-
Open Xcode. In the menu bar, click File > New > Project.
- In the Choose a template for your new project: window, set the following properties:
- Multiplatform iOS
- Application App
- Click Next.
- In the Choose options for your new project: window, set the following properties:
- Product Name:
<your app name
> - Organization Identifier:
<your organization
> - Interface: SwiftUI
- Language: Swift
- Product Name:
- Uncheck all other options.
- Click Next.
- Choose a location to store your project. Click Create.
- In the Choose a template for your new project: 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 > 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 the subsequent tutorials.App -
Add a reference to the API using Swift Package Manager.
Set developer credentials
To allow your app users to access ArcGIS location services, pass the developer credentials that you created in the Set up authentication step to the application's ArcGISEnvironment
.
Pass your API Key access token to the ArcGISEnvironment
.
-
In the Project Navigator, click MainApp.swift.
-
Implement an initializer in the
Main
struct and set theApp ArcGIS
property with your API key access token.Environment.api Key MainApp.swiftUse dark colors for code blocks import SwiftUI import ArcGIS @main struct MainApp: App { init() { ArcGISEnvironment.apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>") }
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. In a production environment we do not recommend that you store it directly in source code.
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 @State property wrapper named
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 }() }
-
Set the map's initialViewpoint property with a
Viewpoint
using the Santa Monica Mountains coordinates.ContentView.swiftUse dark colors for code blocks 22 23 24 26 27 28Add line. @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 30 31 34 35Add line. Add line. var body: some View { // Displays the map. MapView(map: map) }
Run the solution
-
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 Add line. .ignoresSafeArea()
-
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS 14 (Sonoma), Xcode 16, iOS 18. 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.
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solution
-
Click the
Download solution
link under Solution and unzip the file to a location on your machine. -
Open the
.xcodeproj
file in Xcode.
Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the set up authentication section.
Set developer credentials in the solution
To allow your app users to access ArcGIS location services, pass the developer credentials that you created in the Set up authentication step to the application's ArcGISEnvironment
.
Pass your API Key access token to the ArcGISEnvironment
.
-
In the Project Navigator, click MainApp.swift.
-
Set the
Authentication
toMode .api
.Key MainApp.swiftUse dark colors for code blocks // Change the `AuthenticationMode` to `.apiKey` if your application uses API key authentication. private var authenticationMode: AuthenticationMode { .apiKey }
-
Set the
api
property with your API key access token.Key MainApp.swiftUse dark colors for code blocks // Please enter an API key access token if your application uses API key authentication. private let apiKey = APIKey("YOUR-ACCESS-TOKEN")
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. In a production environment we do not recommend that you store it directly in source code.
Run the solution
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS 14 (Sonoma), Xcode 16, iOS 18. 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: