Learn how to create and display a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more .

display a map

A map contains layers A layer is a reference to a collection of geographic data that is used to access and display data. The data for layers are typically provided by the basemap layer service and data services. Learn more of geographic data. A map contains a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more and, optionally, one or more data layers A data layer is a layer that references geographic data from a file or a service and is used to visualize the data in a map or scene. Learn more . You can display a specific area of a map by using a map view A map view is a user interface that displays map layers and graphics in 2D. It controls the area (extent) of the map that is visible and supports user interactions such as pan and zoom. Learn more and setting the location A location is a position or region (point, line, or polygon) on the earth's surface. Learn more and zoom level Zoom level is a value that sets the scale for a map view or a scene view. Learn more .

In this tutorial, you create and display a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more of the Santa Monica Mountains in California using the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more .

The map and code will be used as the starting point for other 2D tutorials.

This tutorial builds a cross-platform app using the .NET Multi-platform App UI (.NET MAUI) framework. .NET MAUI allows you to build native cross-platform apps from a shared code base. If you’d prefer to build this app as a Windows desktop app, see the Windows Presentation Foundation (WPF) version of this tutorial.

Prerequisites

Before starting this tutorial:

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.

Set up authentication

To access the secure ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial, you must implement API key authentication API key authentication is a type of authentication that uses an API key to authenticate requests to ArcGIS services and secure portal items. Learn more or user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more using an ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or an ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more account.

To complete this tutorial, click on the tab in the switcher below for your authentication type of choice, either API key authentication or User authentication.

Create a new API key access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more to access the secure resources used in this tutorial.

  1. Complete the Create an API key tutorial and create an API key with the following privilege(s) Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :

    • Privileges
      • Location services > Basemaps
  2. Copy and paste the API key access token into a safe location. It will be used in a later step.

Develop or download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

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 Multi-platform App UI (.NET MAUI). The instructions for this tutorial are specific to creating a .NET MAUI project using Visual Studio for Windows.

  1. Start Visual Studio and create a new project.

    • In the Visual Studio start screen, click Create a new project.
    • Choose the .NET MAUI App template for C#. If you don’t see the template listed, you can find it by typing MAUI into the Search for templates text box.
    • Click Next.
    • Provide required values in the Configure your new project panel:
      • Project name: DisplayAMap
      • Location: choose a folder
    • Click Next.
      • Choose the .NET 9 as framework you intend to target.
    • Click Create to create the project.

Add a reference to the API

  1. 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.Maui NuGet package.

    • Confirm the Latest stable version of the package is selected in the Version dropdown.

    • Click Apply.

    • 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.

    • Close the NuGet Package Manager window.

Correct minimum OS versions

The .NET MAUI App template may have operating system (OS) requirements below what ArcGIS Maps SDK for .NET requires for a .NET MAUI app. You must modify the minimum OS versions in your .csproj to satisfy the ArcGIS Maps SDK for .NET requirements.

  1. In Visual Studio > Solution Explorer, double-click DisplayAMap to open the .csproj.

  2. Modify the <SupportedOSPlatformVersion> properties in the <PropertyGroup> as shown below. This reflects the minimum OS requirements for ArcGIS Maps SDK for .NET MAUI for the current release.

    DisplayAMap.csproj
    29 collapsed lines
    <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
    <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
    <!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
    <!-- Note for MacCatalyst:
    The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
    When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
    The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
    either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
    <!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
    <OutputType>Exe</OutputType>
    <RootNamespace>DisplayAMap</RootNamespace>
    <UseMaui>true</UseMaui>
    <SingleProject>true</SingleProject>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <!-- Display name -->
    <ApplicationTitle>DisplayAMap</ApplicationTitle>
    <!-- App Identifier -->
    <ApplicationId>com.companyname.displayamap</ApplicationId>
    <ApplicationIdGuid>8861a28a-1665-4549-825a-f7c5ee7dac07</ApplicationIdGuid>
    <!-- Versions -->
    <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
    <ApplicationVersion>1</ApplicationVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">16.0</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">28.0</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</SupportedOSPlatformVersion>
    <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</TargetPlatformMinVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    </PropertyGroup>
    32 collapsed lines
    <ItemGroup>
    <!-- App Icon -->
    <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
    <!-- Splash Screen -->
    <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
    <!-- Images -->
    <MauiImage Include="Resources\Images\*" />
    <MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
    <!-- Custom Fonts -->
    <MauiFont Include="Resources\Fonts\*" />
    <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
    <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>
    <ItemGroup>
    <PackageReference Include="Microsoft.Maui.Controls" Version="9.0.50" />
    <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="9.0.50" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.3" />
    <PackageReference Include="Esri.ArcGISRuntime.Maui" Version="200.8.2" />
    </ItemGroup>
    <ItemGroup Condition="'$(TargetFramework)' == 'net9.0-windows10.0.19041.0'">
    <PackageReference Include="WinUIEx" Version="2.6.0" />
    </ItemGroup>
    </Project>
  3. Save and close the .csproj file.

Set developer credentials

To allow your app users to access ArcGIS Location Services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In the Solution Explorer, open MauiProgram.cs by double-clicking.

  2. Add the following required using directives at the top of the file.

    MauiProgram.cs
    13 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    namespace DisplayAMap;
    using Esri.ArcGISRuntime;
    using Esri.ArcGISRuntime.Maui;
    using Microsoft.Extensions.Logging;
    public static class MauiProgram
    {
    public static MauiApp CreateMauiApp()
    {
    17 collapsed lines
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });
    #if DEBUG
    builder.Logging.AddDebug();
    #endif
    return builder.Build();
    }
    }
  3. Use the existing builder variable in the CreateMauiApp() function to call UseArcGISRuntime(). Use a lambda function to call UseApiKey() on a config parameter to set the ApiKey property on ArcGISRuntimeEnvironment.

    MauiProgram.cs
    25 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    namespace DisplayAMap;
    using Esri.ArcGISRuntime;
    using Esri.ArcGISRuntime.Maui;
    using Microsoft.Extensions.Logging;
    public static class MauiProgram
    {
    public static MauiApp CreateMauiApp()
    {
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });
    builder.UseArcGISRuntime(config =>
    {
    config.UseApiKey("YOUR_ACCESS_TOKEN");
    });
    #if DEBUG
    builder.Logging.AddDebug();
    #endif
    4 collapsed lines
    return builder.Build();
    }
    }
  1. Update the "YOUR_ACCESS_TOKEN" placeholder with the API key access token you created earlier.

  2. Save and close the MauiProgram.cs file.

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Remove default controls

The .NET MAUI App template provided by Visual Studio has default controls and logic. These must be removed to make room for the map view in your UI.

  1. Open the MainPage.xaml file.

  2. Remove the ScrollView container (and all the controls it contains) from the current UI.

    MainPage.xaml
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="DisplayAMap.MainPage">
    <ScrollView>
    <VerticalStackLayout
    Spacing="25"
    Padding="30,0"
    VerticalOptions="Center">
    <Image
    Source="dotnet_bot.png"
    SemanticProperties.Description="Cute dot net bot waving hi to you!"
    HeightRequest="200"
    HorizontalOptions="Center" />
    <Label
    Text="Hello, World!"
    SemanticProperties.HeadingLevel="Level1"
    FontSize="32"
    HorizontalOptions="Center" />
    <Label
    Text="Welcome to .NET Multi-platform App UI"
    SemanticProperties.HeadingLevel="Level2"
    SemanticProperties.Description="Welcome to dot net Multi platform App U I"
    FontSize="18"
    HorizontalOptions="Center" />
    <Button
    x:Name="CounterBtn"
    Text="Click me"
    SemanticProperties.Hint="Counts the number of times you click"
    Clicked="OnCounterClicked"
    HorizontalOptions="Center" />
    </VerticalStackLayout>
    </ScrollView>
    </ContentPage>
  3. Open the MainPage.xaml.cs file.

  4. Remove the OnCounterClicked() function and the count variable.

    MainPage.xaml.cs
    namespace DisplayAMap;
    public partial class MainPage : ContentPage
    {
    int count = 0;
    public MainPage()
    {
    InitializeComponent();
    }
    private void OnCounterClicked(object sender, EventArgs e)
    {
    count++;
    if (count == 1)
    CounterBtn.Text = $"Clicked {count} time";
    else
    CounterBtn.Text = $"Clicked {count} times";
    SemanticScreenReader.Announce(CounterBtn.Text);
    }
    }
  5. Save and close MainPage.xaml and MainPage.xaml.cs.

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.

  1. Add a new class that will define a view model for the project.

    • Click Project > Add Class ….
    • Name the new class MapViewModel.cs.
    • Click Add to create the new class and add it to the project.
    • The new class will open in Visual Studio.
  2. Add required using statements to the view model.

    MapViewModel.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Esri.ArcGISRuntime.Geometry;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
  3. Implement the INotifyPropertyChanged interface in the MapViewModel class.

    This interface defines a PropertyChanged event that is used to notify clients (views) that a property of the view model has changed.

    MapViewModel.cs
    namespace DisplayAMap
    {
    internal class MapViewModel : INotifyPropertyChanged
    {
  4. Inside the MapViewModel class, add code to implement the PropertyChanged event.

    When a property of the view model changes, a call to OnPropertyChanged will raise this event.

    MapViewModel.cs
    28 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Esri.ArcGISRuntime.Geometry;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
    namespace DisplayAMap
    {
    internal class MapViewModel : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    2 collapsed lines
    }
  5. Define a new property on the view model called Map that exposes a Map object. When the property is set, call OnPropertyChanged.

    MapViewModel.cs
    28 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Esri.ArcGISRuntime.Geometry;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
    namespace DisplayAMap
    {
    internal class MapViewModel : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map _map;
    public Map Map
    {
    get => _map;
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    }
    2 collapsed lines
    }
  6. Add a function to the MapViewModel class called SetupMap. This function will create a new map and use it to set the Map property.

    MapViewModel.cs
    48 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Esri.ArcGISRuntime.Geometry;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
    namespace DisplayAMap
    {
    internal class MapViewModel : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map _map;
    public Map Map
    {
    get => _map;
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map = new Map(BasemapStyle.ArcGISTopographic);
    }
    4 collapsed lines
    }
    }
  7. Add code to set the map’s InitialViewpoint property to a Viewpoint.

    MapViewModel.cs
    51 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Esri.ArcGISRuntime.Geometry;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
    namespace DisplayAMap
    {
    internal class MapViewModel : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map _map;
    public Map Map
    {
    get => _map;
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map = new Map(BasemapStyle.ArcGISTopographic);
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    Map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    6 collapsed lines
    }
    }
    }
  8. Add a constructor to the class that calls SetupMap() when a new MapViewModel is instantiated.

    MapViewModel.cs
    25 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Esri.ArcGISRuntime.Mapping;
    using Esri.ArcGISRuntime.Geometry;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Map = Esri.ArcGISRuntime.Mapping.Map;
    namespace DisplayAMap
    {
    internal class MapViewModel : INotifyPropertyChanged
    {
    public MapViewModel()
    {
    SetupMap();
    }
    32 collapsed lines
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    private Map _map;
    public Map Map
    {
    get => _map;
    set
    {
    _map = value;
    OnPropertyChanged();
    }
    }
    private void SetupMap()
    {
    // Create a new map with a 'topographic vector' basemap.
    Map = new Map(BasemapStyle.ArcGISTopographic);
    var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
    Map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
    }
    }
    }
  9. Save and close the MapViewModel.cs file.

Your MapViewModel 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 in the MapViewModel.

  1. Add required XML namespace and resource declarations to MainPage.xaml.

    MainPage.xaml
    2 collapsed lines
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage x:Class="DisplayAMap.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    xmlns:local="clr-namespace:DisplayAMap">
    <ContentPage.Resources>
    <local:MapViewModel x:Key="MapViewModel" />
    </ContentPage.Resources>
    3 collapsed lines
    </ContentPage>
  2. Add a MapView control to MainPage.xaml and bind it to the MapViewModel.

    MainPage.xaml
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage x:Class="DisplayAMap.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    xmlns:local="clr-namespace:DisplayAMap">
    <ContentPage.Resources>
    <local:MapViewModel x:Key="MapViewModel" />
    </ContentPage.Resources>
    <esri:MapView x:Name="MainMapView" Map="{Binding Map, Source={StaticResource MapViewModel}}" />
    </ContentPage>
  3. Save and close the MainPage.xaml file.

Run the app

Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app. If your app uses user authentication, enter your ArcGIS Online credentials when prompted.

Deploy the app to a Windows, Mac, iOS, or Android device. You will see a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more centered on the Santa Monica Mountains in California. Pan and zoom with mouse or touchscreen controls to explore the map.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

  1. Click the Download solution link in the right-hand panel of the page.

  2. Unzip the file to a location on your machine.

  3. Open the .sln file in Visual Studio.

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 ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In the Solution Explorer, open MauiProgram.cs by double-clicking.

  2. Update the call to UseApiKey() on the config parameter to set your API key access token.

    MauiProgram.cs
    25 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    namespace DisplayAMap;
    using Esri.ArcGISRuntime;
    using Esri.ArcGISRuntime.Maui;
    using Microsoft.Extensions.Logging;
    public static class MauiProgram
    {
    public static MauiApp CreateMauiApp()
    {
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });
    builder.UseArcGISRuntime(config =>
    {
    config.UseApiKey("YOUR_ACCESS_TOKEN");
    });
    #if DEBUG
    builder.Logging.AddDebug();
    #endif
    4 collapsed lines
    return builder.Build();
    }
    }
  3. Remove the line of code that sets the user authentication handler.

    MauiProgram.cs
    25 collapsed lines
    // Copyright 2023 Esri
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    namespace DisplayAMap;
    using Esri.ArcGISRuntime;
    using Esri.ArcGISRuntime.Maui;
    using Microsoft.Extensions.Logging;
    public static class MauiProgram
    {
    public static MauiApp CreateMauiApp()
    {
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });
    builder.UseArcGISRuntime(config =>
    {
    config.UseApiKey("YOUR_ACCESS_TOKEN");
    });
    // Set up the AuthenticationManager to use OAuth for secure ArcGIS Online requests.
    UserAuth.ArcGISLoginPrompt.RegisterOAuthConfig();
    #if DEBUG
    builder.Logging.AddDebug();
    #endif
    4 collapsed lines
    return builder.Build();
    }
    }
  4. Save and close the MauiProgram.cs file.

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the app

Click Debug > Start Debugging (or press <F5> on the keyboard) to run the app. If your app uses user authentication, enter your ArcGIS Online credentials when prompted.

Deploy the app to a Windows, Mac, iOS, or Android device. You will see a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more centered on the Santa Monica Mountains in California. Pan and zoom with mouse or touchscreen controls to explore the map.

What’s next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: