Display a map
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.
- Your system meets the system requirements.
Steps
Create a new Xcode project
Use Xcode to create a single view iOS app and configure it to reference the API.
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>
- Product Name:
- Uncheck all other options.
- Click Next > Create.
- In the Choose options window, set the following properties:
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.
In the Project Navigator, click ViewController.swift.
In the editor, add an
import
statement to reference the API and add an@IBOutlet
namedmap
and of typeView AGSMap
. This will provide a reference to the map view that you will create in the storyboard.View ViewController.swiftUse dark colors for code blocks Add line. Add line. AGSMap
is a subclass ofView UIView
.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.
- Type
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.
- Type
In the menu, click View > Inspectors > Show Identity Inspector. In the Inspectors panel, set Custom Class > Class to
AGSMap
.View This sets the type of the new view to
AGSMap
.View 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
AGSMap
view on the storyboard.View This connects the
AGSMap
in the storyboard to theView map
outlet created earlier in theView View
class.Controller
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
setup
. InMap() setup
create anMap() AGSMap
.ViewController.swiftUse dark colors for code blocks 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. Set the
map
property of themap
outlet to the newView AGSMap
.ViewController.swiftUse dark colors for code blocks Add line. In the
View
'sController view
method, callD i d Load setup
once the view has loaded.Map() ViewController.swiftUse dark colors for code blocks Add line.
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 theApp
'sDelegate application(_
method, set the: did Finish Launching With Options: ) api
property on theKey AGSArcGISRuntime
with your API Key.Environment AppDelegate.swiftUse dark colors for code blocks Add line. Add line. 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.
What's next?
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: