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.
Mapping and location services guideFor more background information about the topics in this tutorial, visit Maps (2D) and Basemaps.
PrerequisitesBefore starting this tutorial:
You need an ArcGIS Location Platform or ArcGIS Online account.
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.
Set up authenticationTo access the secure ArcGIS location services used in this tutorial, you must implement API key authentication or user authentication using an ArcGIS Location Platform or an ArcGIS Online account.
You can implement API key authentication or user authentication in this tutorial. Compare the differences below:
API key authentication
Learn more in API key authentication.
User authentication
Learn more in User authentication.
Security and authentication guideTo learn more about the different types of authentication, visit Types of authentication.
Create a new API key access token with privileges to access the secure resources used in this tutorial.
Complete the Create an API key tutorial and create an API key with the following privilege(s):
Copy and paste the API key access token into a safe location. It will be used in a later step.
Create new OAuth credentials to access the secure resources used in this tutorial.
WarningConfiguration steps later in the tutorial will assume that your redirect URL is my-app://auth
. If you use a different URL, make sure to configure your app's settings accordingly.
Complete the Create OAuth credentials for user authentication tutorial to obtain a Client ID and Redirect URL.
A Client ID
uniquely identifies your app on the authenticating server. If the server cannot find an app with the provided Client ID, it will not proceed with authentication.
The Redirect URL
(also referred to as a callback url) is used to identify a response from the authenticating server when the system returns control back to your app after an OAuth login. Since it does not necessarily represent a valid endpoint that a user could navigate to, the redirect URL can use a custom scheme, such as my-app://auth
. It is important to make sure the redirect URL used in your app's code matches a redirect URL configured on the authenticating server.
Copy and paste the Client ID and Redirect URL into a safe location. They will be used in a later step.
All users that access this application need account privileges to access the ArcGIS Basemap Styles service.
Develop or downloadYou have two options for completing this tutorial:
Option 1: Develop the code Create a new Visual Studio ProjectArcGIS 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 WPF .NET project using Visual Studio for Windows.
TopicThe platforms available for which you can develop .NET API apps depends on your development environment. For example, WPF and UWP projects are not available when developing on a Mac. See system requirements for more information.
Start Visual Studio and create a new project.
WPF Application
into the Search for templates text box.DisplayAMap
choose a folder
.NET 8.0 (Long Term Support)
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.
TipThe instructions in this tutorial are specific to creating an application using WPF for .NET (requires Visual Studio 2022 or higher). To complete the app with another supported .NET platform, create your project from one of the ArcGIS Maps SDK for .NET project templates. If starting from one of the Visual Studio templates, you may notice some differences between code in the instructions and the code included in your project.
Add a reference to the API NoteWhen you create a project from one of the ArcGIS Maps SDK for .NET project templates, the required NuGet packages are automatically referenced.
Add a reference to the API by installing a NuGet package.
nuget.org
(upper-right).You may see an error like this in the Visual Studio Error List: The 'Esri.ArcGISRuntime.WPF' nuget package cannot be used to target 'net8.0-windows'. Target 'net8.0-windows10.0.19041.0' or later instead.
. If so, follow these steps to address it.
<TargetFramework>
element with net8.0-windows10.0.19041.0
(or higher).Use dark colors for code blocks
1
2
3
4
5
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
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,model
represents the data consumed in an app, view
represents the user interface, and view model
contains the logic that binds model
and view
together. The framework required for such a pattern might initially seem like a lot of work for a small project, but as your project's complexity increases, a solid design foundation will make your code more flexible and easier to maintain.
In an ArcGIS app designed with MVVM, the map view or scene view usually provides the main view
component. Many of the classes fill the role of model
(representing data as maps, scenes, layers, graphics, features, and others). Much of the code you write will be for the view model
component, where you will add logic to work with ArcGIS objects and provide data for display in the view
.
All of the ArcGIS Maps SDK for .NET project templates use the MVVM design pattern.
Add a new class that will define a view model for the project.
MapViewModel.cs
.Add required using
statements to the view model.
MapViewModel.cs
Use dark colors for code blocks
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Esri.ArcGISRuntime.Mapping;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Esri.ArcGISRuntime.Geometry;
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
Use dark colors for code blocks
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
namespace DisplayAMap
{
internal class MapViewModel : INotifyPropertyChanged
{
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
Expand
Use dark colors for code blocks1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
internal class MapViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Define a new property on the view model called Map
that exposes a Map
object. When the property is set, call OnPropertyChanged
.
MapViewModel.cs
Expand
Use dark colors for code blocks1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 { return _map; }
set
{
_map = value;
OnPropertyChanged();
}
}
}
Add a function to the MapViewModel
class called SetupMap
. This function will create a new map and use it to set the Map
property.
The map will use one of the secured ArcGIS basemap styles and center on the Santa Monica Mountains in California.
MapViewModel.cs
Expand
Use dark colors for code blocks1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
private void SetupMap()
{
// Create a new map with a 'topographic vector' basemap.
var map = new Map(BasemapStyle.ArcGISTopographic);
// Set the initial viewpoint around the Santa Monica Mountains in California.
var mapCenterPoint = new MapPoint(-118.805, 34.027, SpatialReferences.Wgs84);
map.InitialViewpoint = new Viewpoint(mapCenterPoint, 100000);
// Set the view model's Map property with the map.
Map = map;
}
Add a constructor to the class that calls SetupMap
when a new MapViewModel
is instantiated.
When you write code like MapViewModel newMapVM = new MapViewModel();
, the class constructor runs. This is a good place to add code that needs to run when the class is initialized.
MapViewModel.cs
Expand
Use dark colors for code blocks1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
namespace DisplayAMap
{
internal class MapViewModel : INotifyPropertyChanged
{
public MapViewModel()
{
SetupMap();
}
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. This view model could be used in a .NET MAUI app, a WinUI app, or a UWP app with little or no modification.
To allow your app users to access ArcGIS Location Services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.
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 OnStartup()
function to set the ApiKey
property on ArcGISRuntimeEnvironment
.
App.xaml.cs
Use dark colors for code blocks
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
41
42
43
44
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Set the access token for ArcGIS Maps SDK for .NET.
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
}
}
}
Replace "YOUR_ACCESS_TOKEN" with the API key access token you created earlier.
Save and close the App.xaml.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.
From the Visual Studio Project menu, choose Add class .... Name the class ArcGISLoginPrompt.cs
then click Add. The new class is added to your project and opens in Visual Studio.
Select all the code in the new class and delete it.
Copy all of the code below and paste it into the ArcGISLoginPrompt.cs
class in your project.
ArcGISLoginPrompt.cs
Use dark colors for code blocks Copy
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2021 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: http://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 Esri.ArcGISRuntime.Security;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
namespace UserAuth
{
internal static class ArcGISLoginPrompt
{
private const string ArcGISOnlineUrl = "https://www.arcgis.com/sharing/rest";
// Specify the Client ID and Redirect URL to use with OAuth authentication.
// See the instructions here for creating OAuth app settings:
// https://developers.arcgis.com/documentation/security-and-authentication/user-authentication/tutorials/create-oauth-credentials-user-auth/
private const string AppClientId = "YOUR_CLIENT_ID";
private const string OAuthRedirectUrl = "YOUR_REDIRECT_URL";
public static async Task<bool> EnsureAGOLCredentialAsync()
{
bool loggedIn = false;
try
{
// Create a challenge request for portal credentials (OAuth credential request for arcgis.com)
CredentialRequestInfo challengeRequest = new CredentialRequestInfo
{
// Use the OAuth authorization code workflow.
GenerateTokenOptions = new GenerateTokenOptions
{
TokenAuthenticationType = TokenAuthenticationType.OAuthAuthorizationCode
},
// Indicate the url (portal) to authenticate with (ArcGIS Online)
ServiceUri = new Uri(ArcGISOnlineUrl)
};
// Call GetCredentialAsync on the AuthenticationManager to invoke the challenge handler
Credential? cred = await AuthenticationManager.Current.GetCredentialAsync(challengeRequest, false);
loggedIn = cred != null;
}
catch (OperationCanceledException)
{
// OAuth login was canceled, no need to display error to user.
}
catch (Exception ex)
{
// Login failure
MessageBox.Show("Login failed: " + ex.Message);
}
return loggedIn;
}
public static void SetChallengeHandler()
{
var userConfig = new OAuthUserConfiguration(new Uri(ArcGISOnlineUrl), AppClientId, new Uri(OAuthRedirectUrl));
AuthenticationManager.Current.OAuthUserConfigurations.Add(userConfig);
AuthenticationManager.Current.OAuthAuthorizeHandler = new OAuthAuthorize();
}
#region OAuth handler
// In a desktop (WPF) app, an IOAuthAuthorizeHandler component is used to handle some of the OAuth details. Specifically, it
// implements AuthorizeAsync to show the login UI (generated by the server that hosts secure content) in a web control.
// When the user logs in successfully, cancels the login, or closes the window without continuing, the IOAuthAuthorizeHandler
// is responsible for obtaining the authorization from the server or raising an OperationCanceledException.
public class OAuthAuthorize : IOAuthAuthorizeHandler
{
// Window to contain the OAuth UI.
private Window? _authWindow;
// Use a TaskCompletionSource to track the completion of the authorization.
private TaskCompletionSource<IDictionary<string, string>> _tcs;
// URL for the authorization callback result (the redirect URI configured for your application).
private string _callbackUrl = "";
// URL that handles the OAuth request.
private string? _authorizeUrl;
// Function to handle authorization requests, takes the URIs for the secured service, the authorization endpoint, and the redirect URI.
public Task<IDictionary<string, string>> AuthorizeAsync(Uri serviceUri, Uri authorizeUri, Uri callbackUri)
{
if (_tcs != null && !_tcs.Task.IsCompleted)
throw new Exception("Task in progress");
_tcs = new TaskCompletionSource<IDictionary<string, string>>();
// Store the authorization and redirect URLs.
_authorizeUrl = authorizeUri.AbsoluteUri;
_callbackUrl = callbackUri.AbsoluteUri;
// Call a function to show the login controls, make sure it runs on the UI thread for this app.
Dispatcher dispatcher = Application.Current.Dispatcher;
if (dispatcher == null || dispatcher.CheckAccess())
{
AuthorizeOnUIThread(_authorizeUrl);
}
else
{
Action authorizeOnUIAction = () => AuthorizeOnUIThread(_authorizeUrl);
dispatcher.BeginInvoke(authorizeOnUIAction);
}
// Return the task associated with the TaskCompletionSource.
return _tcs.Task;
}
// Challenge for OAuth credentials on the UI thread.
private void AuthorizeOnUIThread(string authorizeUri)
{
// Create a WebBrowser control to display the authorize page.
WebBrowser webBrowser = new WebBrowser();
// Handle the navigation event for the browser to check for a response to the redirect URL.
webBrowser.Navigating += WebBrowserOnNavigating;
// Display the web browser in a new window.
_authWindow = new Window
{
Content = webBrowser,
Width = 450,
Height = 450,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
// Set the app's window as the owner of the browser window (if main window closes, so will the browser).
if (Application.Current != null && Application.Current.MainWindow != null)
{
_authWindow.Owner = Application.Current.MainWindow;
}
// Handle the window closed event then navigate to the authorize url.
_authWindow.Closed += OnWindowClosed;
webBrowser.Navigate(authorizeUri);
// Display the window.
_authWindow.ShowDialog();
}
// Handle the browser window closing.
private void OnWindowClosed(object? sender, EventArgs e)
{
// If the browser window closes, return the focus to the main window.
if (_authWindow != null && _authWindow.Owner != null)
{
_authWindow.Owner.Focus();
}
// If the task wasn't completed, the user must have closed the window without logging in.
if (!_tcs.Task.IsCompleted)
{
// Set the task completion source exception to indicate a canceled operation.
_tcs.SetCanceled();
}
_authWindow = null;
}
// Handle browser navigation (content changing).
private void WebBrowserOnNavigating(object sender, NavigatingCancelEventArgs e)
{
// Check for a response to the callback url.
const string portalApprovalMarker = "/oauth2/approval";
WebBrowser? webBrowser = sender as WebBrowser;
Uri uri = e.Uri;
// If no browser, uri, or an empty url, return.
if (webBrowser == null || uri == null || string.IsNullOrEmpty(uri.AbsoluteUri))
return;
// Check for redirect.
bool isRedirected = uri.AbsoluteUri.StartsWith(_callbackUrl) ||
_callbackUrl.Contains(portalApprovalMarker) && uri.AbsoluteUri.Contains(portalApprovalMarker);
// Check if browser was redirected to the callback URL. (This indicates succesful authentication.)
if (isRedirected)
{
e.Cancel = true;
// Call a helper function to decode the response parameters.
IDictionary<string, string> authResponse = DecodeParameters(uri);
// Set the result for the task completion source.
_tcs.SetResult(authResponse);
// Close the window.
if (_authWindow != null)
{
_authWindow.Close();
}
}
}
private static IDictionary<string, string> DecodeParameters(Uri uri)
{
// Create a dictionary of key value pairs returned in an OAuth authorization response URI query string.
string answer = "";
// Get the values from the URI fragment or query string.
if (!string.IsNullOrEmpty(uri.Fragment))
{
answer = uri.Fragment.Substring(1);
}
else
{
if (!string.IsNullOrEmpty(uri.Query))
{
answer = uri.Query.Substring(1);
}
}
// Parse parameters into key / value pairs.
Dictionary<string, string> keyValueDictionary = new Dictionary<string, string>();
string[] keysAndValues = answer.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string kvString in keysAndValues)
{
string[] pair = kvString.Split('=');
string key = pair[0];
string value = string.Empty;
if (key.Length > 1)
{
value = Uri.UnescapeDataString(pair[1]);
}
keyValueDictionary.Add(key, value);
}
// Return the dictionary of string keys/values.
return keyValueDictionary;
}
}
#endregion OAuth handler
}
}
Add your values for the client ID (AppClientId
) and for the redirect URL (OAuthRedirectUrl
). These are the user authentication settings you created in the Set up authentication step.
ArcGISLoginPrompt.cs
Use dark colors for code blocks
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
internal static class ArcGISLoginPrompt
{
private const string ArcGISOnlineUrl = "https://www.arcgis.com/sharing/rest";
// Specify the Client ID and Redirect URL to use with OAuth authentication.
// See the instructions here for creating OAuth app settings:
// https://developers.arcgis.com/documentation/security-and-authentication/user-authentication/tutorials/create-oauth-credentials-user-auth/
private const string AppClientId = "YOUR_CLIENT_ID";
private const string OAuthRedirectUrl = "YOUR_REDIRECT_URL";
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 OnStartup()
function to call the SetChallengeHandler()
method on your static ArcGISLoginPrompt
class.
App.xaml.cs
Use dark colors for code blocks
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
41
42
43
44
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Call a function to set up the AuthenticationManager for OAuth.
UserAuth.ArcGISLoginPrompt.SetChallengeHandler();
}
}
}
Save and close the App.xaml.cs
file.
Best Practice: The OAuth credentials are stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Next, you will set up a view
in your project to consume the view model.
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 MapViewModel
.
Add required XML namespace and resource declarations.
MainWindow.xaml
and switch to the XAML viewesri
XML namespace for the ArcGIS controlsMapViewModel
instance as a static resourceMainWindow.xaml
Use dark colors for code blocks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<Window x:Class="DisplayAMap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DisplayAMap"
xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MapViewModel x:Key="MapViewModel" />
</Window.Resources>
Add a MapView
control to MainWindow.xaml
and bind it to the MapViewModel
.
MapView
control named MainMapView
Map
property of the control using the MapViewModel
resource.MainWindow.xaml
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<Grid>
<esri:MapView x:Name="MainMapView"
Map="{Binding Map, Source={StaticResource MapViewModel}}" />
</Grid>
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.
You will 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.
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solutionClick the Download solution link in the right-hand panel of the page.
Unzip the file to a location on your machine.
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 solutionTo allow your app users to access ArcGIS location services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.
In Visual Studio, in the Solution Explorer, click App.xaml.cs to open the file.
Set the ArcGISEnvironment.ApiKey
property with your API key access token.
App.xaml.cs
Use dark colors for code blocks
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
41
42
43
44
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Set the access token for ArcGIS Maps SDK for .NET.
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
// Call a function to set up the AuthenticationManager for OAuth.
UserAuth.ArcGISLoginPrompt.SetChallengeHandler();
}
Remove the code that sets up user authentication.
App.xaml.cs
Use dark colors for code blocks
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
41
42
43
44
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Set the access token for ArcGIS Maps SDK for .NET.
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
// Call a function to set up the AuthenticationManager for OAuth.
UserAuth.ArcGISLoginPrompt.SetChallengeHandler();
}
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.
From the Visual Studio Solution explorer window, open the ArcGISLoginPrompt.cs
file.
Set your values for the client ID (OAuthClientID
) and the redirect URL (OAuthRedirectUrl
). These are the user authentication settings you created in the Set up authentication step.
ArcGISLoginPrompt.cs
Use dark colors for code blocks
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
internal static class ArcGISLoginPrompt
{
private const string ArcGISOnlineUrl = "https://www.arcgis.com/sharing/rest";
// Specify the Client ID and Redirect URL to use with OAuth authentication.
// See the instructions here for creating OAuth app settings:
// https://developers.arcgis.com/documentation/security-and-authentication/user-authentication/tutorials/create-oauth-credentials-user-auth/
private const string AppClientId = "YOUR_CLIENT_ID";
private const string OAuthRedirectUrl = "YOUR_REDIRECT_URL";
In Visual Studio, in the Solution Explorer, click App.xaml.cs to open the file.
Remove the line of code that sets an API key access token.
App.xaml.cs
Use dark colors for code blocks
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
41
42
43
44
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Set the access token for ArcGIS Maps SDK for .NET.
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";
// Call a function to set up the AuthenticationManager for OAuth.
UserAuth.ArcGISLoginPrompt.SetChallengeHandler();
}
Best Practice: The OAuth credentials are 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 appClick 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.
You will 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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4