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
Before starting this tutorial, you should:
Have an ArcGIS account and an API key to access ArcGIS services. If you don't have an account, sign up for free.
Optionally, you may want to install the ArcGIS Maps SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.
Steps
Open a Visual Studio solution
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
Open the .sln file in Visual Studio.
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.
If necessary, set the API Key.
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 Visual Studio, in the Solution Explorer, click App.xaml.cs.
In the App class, add an override for the OnStartup() function to set the ApiKey property on ArcGISRuntimeEnvironment.
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
publicpartialclassApp : Application {
protectedoverridevoidOnStartup(StartupEventArgs e) {
base.OnStartup(e);
// 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. Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_API_KEY";
}
}
}
If you are developing with Visual Studio for Windows, ArcGIS Maps SDK for .NET provides a set of project templates for each supported .NET platform. These templates follow the Model-View-ViewModel (MVVM) design pattern. Install the ArcGIS Maps SDK for .NET Visual Studio Extension to add the templates to Visual Studio (Windows only). See Install and set up for details.
Update the tutorial name used in the project (optional)
The Visual Studio solution, project, and the namespace for all classes currently use the name DisplayAMap. Follow the steps below if you prefer the name to reflect the current tutorial. These steps are not required, your code will still work if you keep the original name.
The tutorial instructions and code use the name FindARouteAndDirections for the solution, project, and namespace. You can choose any name you like, but it should be the same for each of these.
Update the name for the solution and the project.
In Visual Studio, in the Solution Explorer, right-click the solution name and choose Rename. Provide the new name for your solution.
In the Solution Explorer, right-click the project name and choose Rename. Provide the new name for your project.
Rename the namespace used by classes in the project.
In the Solution Explorer, expand the project node.
Double-click MapViewModel.cs in the Solution Explorer to open the file.
In the MapViewModel class, double-click the namespace name (DisplayAMap) to select it, and then right-click and choose Rename....
Provide the new name for the namespace.
Click Apply in the Rename: DisplayAMap window that appears in the upper-right of the code window. This will rename the namespace throughout your project.
Build the project.
Choose Build > Build solution (or press <F6>).
Add a graphics overlay
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 the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
The project uses the Model-View-ViewModel (MVVM) design pattern to separate the application logic (view model) from the user interface (view). MapViewModel.cs contains the view model class for the application, called MapViewModel. See the Microsoft documentation for more information about the Model-View-ViewModel pattern.
Add additional required using statements to your MapViewModel class. Esri.ArcGISRuntime.UI namespace contains the Graphic class and Esri.ArcGISRuntime.Symbology namespace contains the classes that define the symbols for displaying them.
In the view model, create a new property named GraphicsOverlays. This will be a collection of GraphicsOverlay that will store point, line, and polygon graphics.
In the Visual Studio > Solution Explorer, double-click MainWindow.xaml to open the file.
Use data binding to bind the GraphicsOverlays property of the MapViewModel to the MapView control.
Data binding and the Model-View-ViewModel (MVVM) design pattern allow you to separate the logic in your app (the view model) from the presentation layer (the view). Graphics can be added and removed from the graphics overlays in the view model and those changes appear in the map view through data binding.
A point graphic is created using a map point and a marker symbol. A map point is defined with x and y coordinates and a spatial reference. For latitude and longitude coordinates, the spatial reference is WGS84.
In the Visual Studio > Solution Explorer, double-click MapViewModel.cs to open the file.
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
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, and 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.
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
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 that comprise a closed boundary. For a single area with no openings, you can use the Polygon constructor to create a polygon made of just one part. To create a polygon that consists of more than one part, use a PolygonBuilder.