Learn how to display point, line, and polygon graphics in a map.
You typically use graphics to display geographic data that is not connected to a database and that is not persisted, like highlighting a route between two locations, displaying a search buffer around a point, or tracking the location of a vehicle in real-time. Graphics are composed of a geometry, symbol, and attributes.
In this tutorial, you display points, lines, and polygons on a map as graphics.
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.
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
Open the .xcodeproj file in Xcode.
If you downloaded the solution project, set your API key.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
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 Xcode, in the Project Navigator, click MainApp.swift.
In the Editor, set the ArcGISEnvironment.apiKey property on the ArcGISEnvironment with your API key.
A graphics overlay is a container for graphics. It is used with a map view to display graphics on a map. You can add more than one graphics overlay to a map view. Graphics overlays are displayed on top of all the other layers.
In Xcode, in the Project navigator, click ContentView.swift.
Create a class called Model that adopts the ObservableObject protocol. Within that class, create a GraphicsOverlay as a default value. Initialize the property with an empty initializer and return it. You'll edit this method at a later step to add all of the graphics to display.
In the body, update the map view initializer by adding the graphicsOverlays parameter. Pass in the graphicsOverlayModel's graphicOverlay property to add the graphics overlay created in the previous steps to the map view's list of graphics overlays.
A point graphic is created using a point and a marker symbol. A point is defined with x and y coordinates, and a spatial reference. For latitude and longitude coordinates, the spatial reference is WGS84.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Monterey 12.5, Xcode 14.0, iOS 16. If you are using a physical device, then refer to the system requirements.
You should see a point graphic in Point Dume State Beach.
Add a line graphic
A line graphic is created using a polyline and a line symbol. A polyline is defined as a sequence of points.
Polylines have one or more distinct parts. Each part is a sequence of points. For a continuous line, you can use the Polyline constructor to create a polyline with just one part. To create a polyline with more than one part, use a PolylineBuilder.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Monterey 12.5, Xcode 14.0, iOS 16. If you are using a physical device, then refer to the system requirements.
You should see a point and line graphic along Westward Beach.
Add a polygon graphic
A polygon graphic is created using a polygon and a fill symbol. A polygon is defined as a sequence of points that describe a closed boundary.
Polygons have one or more distinct parts. Each part is a sequence of points describing a closed boundary. For a single area with no holes, you can use the Polygon constructor to create a polygon with just one part. To create a polygon with more than one part, use a PolygonBuilder.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Monterey 12.5, Xcode 14.0, iOS 16. If you are using a physical device, then refer to the system requirements.
You should see a point, line, and polygon graphic around Mahou Riviera in the Santa Monica Mountains.