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
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.
- Ensure your development environment meets the system requirements.
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
Create a new Visual Studio Project
ArcGIS Maps SDK for .NET supports apps for Windows Presentation Framework (WPF), Universal Windows Platform (UWP), Windows UI Library (WinUI), and .NET MAUI. The instructions for this tutorial are specific to creating a .NET 6 WPF project using Visual Studio for Windows.
Start Visual Studio and create a new project.
- In the Visual Studio start screen, click Create a new project.
- Choose the WPF Application template for C#. If you don't see the template listed, you can find it by typing
WPF Application
into the Search for templates text box. - Click Next.
- Provide required values in the Configure your new project panel:
- Project name:
Display
AMap - Location:
choose a folder
- Project name:
- Click Next.
- Choose the framework:
.NET 6.0 (Long Term Support)
- Choose the framework:
- Click Create to create the project.
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.
Add a reference to the API
Add a reference to the API by installing a NuGet package.
- In Solution Explorer, right-click Dependencies and choose Manage NuGet Packages.
- In the NuGet Package Manager window, ensure the selected Package source is
nuget.org
(upper-right). - Select the Browse tab and search for ArcGIS Maps SDK.
- In the search results, select the appropriate package for your platform. For this tutorial project, choose the Esri.ArcGISRuntime.WPF NuGet package.
- Confirm the Latest stable version of the package is selected in the Version dropdown.
- Click Install.
- The Preview Changes dialog confirms any package(s) dependencies or conflicts. Review the changes and click OK to continue installing the packages.
- Review the license information on the License Acceptance dialog and click I Accept to add the package(s) to your project.
- In the Visual Studio Output window, ensure the packages were successfully installed. If you see an error about the target Windows version, you will fix that in the next step.
- Close the NuGet Package Manager window.
You may see an error like this in the Visual Studio Error List:
The 'Esri.ArcGISRuntime.WPF' nuget package cannot be used to target 'net6.0-windows'. Target 'net6.0-windows10.0.19041.0' or later instead.
. If so, follow these steps to address it.In Solution Explorer, right-click the DisplayAMap project entry in the tree view and choose Edit Project File.
Update the
<
element withTarget Framework> net6.0-windows10.0.19041.0
(or higher).Use dark colors for code blocks Change line Save the project file (
Display
) and close it.AMap
Get an 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.
You will use this API Key below.
Set the API Key in your app
In the Solution Explorer, expand the node for App.xaml, and double-click App.xaml.cs to open it.
In the App class, add an override for the
O
function to set then Startup() Api
property onKey ArcGISRuntimeEnvironment
.App.xaml.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Save and close the
App.xaml.cs
file.
Create a view model to store app logic
This app is the foundation for many following tutorials so it's good to build it with a solid design.
The Model-View-ViewModel (MVVM) design pattern provides an architecture that separates user interface elements (and related code) from the underlying app logic. In this pattern, the model
represents the data consumed in an app, the view
is the user interface, and the view model
contains the logic that binds the models and views together. The extra framework required for such a pattern might seem like a lot of work for a small project, but as the complexity of your project grows, a solid design can make your code much more maintainable and flexible.
In an ArcGIS app designed with MVVM, the map view usually provides the main view component. Many of the classes fill the role of models (representing data as maps, layers, graphics, features, and others). Much of the code you write will be for the view model component, since this is where you will add logic to work with ArcGIS objects and provide data for display in the view.
Add a new class that will define a view model for the project.
- Click Project > Add Class ....
- Name the new class
Map
.View Model.cs - Click Add to create the new class and add it to the project.
- The new class will open in Visual Studio.
Add required
using
statements to the view model.MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Implement the
INotify
interface in theProperty Changed Map
class.View Model This interface defines a
Property
event that is used to notify clients (views) that a property of the view model has changed.Changed MapViewModel.csUse dark colors for code blocks Change line Change line Inside the
Map
class, add code to implement theView Model Property
event.Changed When a property of the view model changes, a call to
O
will raise this event.n Property Changed MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Define a new property on the view model called
Map
that exposes aMap
object. When the property is set, callO
.n Property Changed MapViewModel.csUse 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 a function to the
Map
class calledView Model Setup
. This function will create a new map and use it to set theMap Map
property.The map will use a topographic vector tiled basemap.
MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add a constructor to the class that calls
Setup
when a newMap Map
is instantiated.View Model When you write code like
Map
, the class constructor runs. This is a good place to add code that needs to run when the class is initialized.View Model new M a p VM = new Map View Model(); MapViewModel.csUse dark colors for code blocks Add line. Add line. Add line. Add line.
Your Map
is complete!
An advantage of using the MVVM design pattern is the ability to reuse code in a view model.
Because this API has a nearly-standard API surface across platforms, a view model
written for one app typically works on all supported .NET platforms.
Next, you will set up a view
in your project to consume the view model.
Add a map view
A MapView
control is used to display a map. You will add a map view to your project UI and wire it up to consume
the map that is defined on Map
.
Add required XML namespace and resource declarations.
- Open
Main
and switch to the XAML viewWindow.xaml - Inside the existing namespace declarations, add an
esri
XML namespace for the ArcGIS controls - Add XAML that defines a
Map
instance as a static resourceView Model
MainWindow.xamlUse dark colors for code blocks Add line. Add line. Add line. Add line. - Open
Add a
MapView
control toMain
and bind it to theWindow.xaml Map
.View Model - Add XAML that defines a
Map
control namedView Main
M a p View - Use data binding to set the
Map
property of the control using theMap
resource.View Model
MainWindow.xamlUse dark colors for code blocks Add line. Add line. - Add XAML that defines a
Set a viewpoint on the map view
Set a viewpoint for the map view when the window loads. The viewpoint defines a point and map scale that centers the display on the Santa Monica mountains along the coast of southern California.
Open
Main
. This is the code behind file that contains code associated withWindow.xaml.cs Main
and the user interface element it defines.Window.xaml Add new required usings:
MainWindow.xaml.csUse dark colors for code blocks Add line. Add line. In the constructor for
Main
, add code to define a newWindow Viewpoint
and apply it to the map view.MainWindow.xaml.csUse dark colors for code blocks Add line. Add line.
Run the app
Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app.
You should see a map with the topographic basemap layer centered on the Santa Monica Mountains in California. Double-click, drag, and scroll the mouse wheel over 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: