Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Display device location

This code sample is available for these platforms:
View Sample on GitHub

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

Image of display device location

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.


compass, GPS, location, map, mobile, navigation

Sample Code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        <Button Clicked="OnStopClicked" Text="Stop" Grid.Row="0" Grid.Column="0" x:Name="StopButton" />
        <Button Clicked="OnStartClicked" Text="Start" Grid.Row="0" Grid.Column="1" x:Name="StartButton" />
        <esriUI:MapView x:Name="MyMapView" Grid.Row="1" Grid.ColumnSpan="2" />
// Copyright 2016 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:
// 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.Diagnostics;
using System.Linq;
using Xamarin.Forms;
using ArcGISRuntime.Droid;


namespace ArcGISRuntime.Samples.DisplayDeviceLocation
        name: "Display device location",
        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 : ContentPage, IDisposable
        // String array to store the different device location options.
        private string[] _navigationTypes =

        public DisplayDeviceLocation()

        private void Initialize()
            // Create new Map with basemap.
            Map myMap = new Map(Basemap.CreateTopographic());

            // Assign the map to the MapView.
            MyMapView.Map = myMap;

        private void OnStopClicked(object sender, EventArgs e)
            MyMapView.LocationDisplay.IsEnabled = false;

        private async void OnStartClicked(object sender, EventArgs e)
            // Show sheet and get title from the selection.
            string selectedMode =
                await ((Page) Parent).DisplayActionSheet("Select navigation mode", "Cancel", null, _navigationTypes);

            // If selected cancel do nothing.
            if (selectedMode == "Cancel") return;

            // Get index that is used to get the selected url.
            int selectedIndex = _navigationTypes.ToList().IndexOf(selectedMode);

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

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

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

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

                // Permission request only needed on Android.
                // See implementation in MainActivity.cs in the Android platform project.
                await MyMapView.LocationDisplay.DataSource.StartAsync();
                MyMapView.LocationDisplay.IsEnabled = true;
            catch (Exception ex)
                await Application.Current.MainPage.DisplayAlert("Couldn't start location", ex.Message, "OK");

        public void Dispose()
            // Stop the location data source.