Display device location with autopan modes

View inWPFWinUIMAUIUWPView on GitHubSample viewer app

Display your current position on the map, as well as switch between different types of auto pan Modes.

Image of display device location with autopan modes

Use case

When using a map within a GIS, it may be helpful for a user to know their own location within a map, whether that's to aid the user's navigation or to provide an easy means of identifying/collecting geospatial information at their location.

How to use the sample

Select an autopan mode, then use the buttons to start and stop location display.

How it works

  1. Create a MapView.
  2. Get the LocationDisplay object by calling getLocationDisplay() on the map view.
  3. Use start() and stop() on the LocationDisplay object as necessary.

Relevant API

  • LocationDisplay
  • LocationDisplay.AutoPanMode
  • Map
  • MapView

Additional information

Location permissions are required for this sample.

Tags

compass, GPS, location, map, mobile, navigation

Sample Code

DisplayDeviceLocation.xaml.csDisplayDeviceLocation.xaml.csDisplayDeviceLocation.xaml
Use dark colors for code blocksCopy
                                                                                                                            
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
// Copyright 2018 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.Mapping;
using Esri.ArcGISRuntime.UI;
using System;
using System.Windows;

namespace ArcGIS.WPF.Samples.DisplayDeviceLocation
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Display device location with autopan modes",
        category: "Location",
        description: "Display your current position on the map, as well as switch between different types of auto pan Modes.",
        instructions: "Select an autopan mode, then use the buttons to start and stop location display.",
        tags: new[] { "GPS", "compass", "location", "map", "mobile", "navigation" })]
    public partial class DisplayDeviceLocation
    {
        // String array to store the different device location options.
        private string[] _navigationTypes = Enum.GetNames(typeof(LocationDisplayAutoPanMode));

        public DisplayDeviceLocation()
        {
            InitializeComponent();

            // Create the UI, setup the control references and execute initialization
            Initialize();
        }

        private void Initialize()
        {
            // Add event handler for when this sample is unloaded.
            Unloaded += SampleUnloaded;

            // Create new Map with basemap
            Map myMap = new Map(BasemapStyle.ArcGISImageryStandard);

            // Provide used Map to the MapView
            MyMapView.Map = myMap;

            // Set navigation types as items source and set default value
            ModeChooser.ItemsSource = _navigationTypes;
            ModeChooser.SelectedIndex = 0;

            // Update the UI when the user pans the view, changing the location mode
            MyMapView.LocationDisplay.AutoPanModeChanged += (sender, args) =>
            {
                switch (MyMapView.LocationDisplay.AutoPanMode)
                {
                    case LocationDisplayAutoPanMode.Off:
                        ModeChooser.SelectedIndex = 0;
                        break;

                    case LocationDisplayAutoPanMode.Recenter:
                        ModeChooser.SelectedIndex = 1;
                        break;

                    case LocationDisplayAutoPanMode.Navigation:
                        ModeChooser.SelectedIndex = 2;
                        break;

                    case LocationDisplayAutoPanMode.CompassNavigation:
                        ModeChooser.SelectedIndex = 3;
                        break;
                }
            };
        }

        private void OnStartButtonClicked(object sender, RoutedEventArgs e)
        {
            MyMapView.LocationDisplay.IsEnabled = true;
        }

        private void OnStopButtonClicked(object sender, RoutedEventArgs e)
        {
            MyMapView.LocationDisplay.IsEnabled = false;
        }

        private void OnModeChooserSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            // Get index that is used to get the selected URL
            int selectedIndex = ModeChooser.SelectedIndex;

            switch (selectedIndex)
            {
                case 0:
                    // Starts location display with auto pan mode set to Off
                    MyMapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.Off;
                    MyMapView.LocationDisplay.IsEnabled = true;
                    break;

                case 1:
                    // Starts location display with auto pan mode set to Re-center
                    MyMapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.Recenter;
                    MyMapView.LocationDisplay.IsEnabled = true;
                    break;

                case 2:
                    // Starts location display with auto pan mode set to Navigation
                    MyMapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.Navigation;
                    MyMapView.LocationDisplay.IsEnabled = true;
                    break;

                case 3:
                    // Starts location display with auto pan mode set to Compass Navigation
                    MyMapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.CompassNavigation;
                    MyMapView.LocationDisplay.IsEnabled = true;
                    break;
            }
        }

        private void SampleUnloaded(object sender, RoutedEventArgs e)
        {
            // Stop the location data source.
            MyMapView.LocationDisplay?.DataSource?.StopAsync();
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.