Skip To Content

Show device location

In this topic

The ArcGIS Runtime SDK contains classes that allow you to display your current location as a point on the map with minimal development effort. You can also create custom classes to act as location providers in your app by accessing peripheral devices (connected via serial port or Bluetooth) such as GPS, laser range finders, and so on.

Overview of the location classes

The following classes for showing device location are available in the Esri.ArcGISRuntime.Location namespace:

  • LocationDisplay—Used to set the MapView.LocationDisplay property, this class contains properties for defining the location provider, location symbols, the auto pan mode to use when the location changes, and so on. Location display can be enabled and disabled as needed in your app.
  • LocationInfo—Contains location information received from the location provider. It contains properties that describe the position (map point), speed, positional accuracy, and course (heading) for the current location.
  • SystemLocationProvider—Location provider built in to the device. This is the default location provider if one is not explicitly specified. You can handle the LocationChanged event to get new locations as they come in from the provider.

LocationDisplay is the class that you'll use to enable location tracking in your app and to set display preferences. The following are some of the common properties you'll use:

  • AutoPanMode—Describes how the map should react when a new location is displayed: Off (the map does not pan to the current location), Default (the map recenters the display on the current location), or Navigation (the map pans and rotates to show the current location at the center of the map with the heading towards the top of the display).
  • CourseSymbol—The MarkerSymbol used to display the current location when it is moving.
  • DefaultSymbol—The MarkerSymbol used to display the current location when it is stationary.
  • IsEnabled—Whether or not the location is currently being displayed.
  • IsPingAnimationEnabled—Whether or not an animation is displayed as new locations are received.
  • LocationProvider—Class that implements the ILocationProvider interface to provide location updates. By default, this is the SystemLocationProvider but can be set to a custom location provider.

Define location display

Location display can be defined at design time using XAML or at run time with C# or VB.NET. A common workflow is to use XAML to define location display properties for your app, and then work with location programmatically to start or stop location tracking, handle location changed events, and so on. To display and update the current location on the map, however, requires no additional code. The following example defines location display for the map view using XAML. Static resources are defined in the page for symbols used for location display.

<Window.Resources>
    <esri:SimpleMarkerSymbol x:Key="NotMovingMarkerSym" Color="Red" Style="X" Size="16"/>
    <esri:SimpleMarkerSymbol x:Key="MovingMarkerSym" Color="Blue" Style="Circle" Size="12"/>
</Window.Resources>
...
<esri:MapView x:Name="MyMapView">
    <esri:MapView.LocationDisplay>
        <esri:LocationDisplay 
            AutoPanMode="Navigation" 
            DefaultSymbol="{StaticResource NotMovingMarkerSym}" CourseSymbol="{StaticResource MovingMarkerSym}"
            IsEnabled="True" IsPingAnimationEnabled="True">
        </esri:LocationDisplay>
    </esri:MapView.LocationDisplay>
    <esri:Map>
        <esri:ArcGISTiledMapServiceLayer ID="Basemap" 
                      ServiceUri="http://services.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer"/>
    </esri:Map>
</esri:MapView>

Location display

You can turn location display on and off as your app is running by setting the LocationDisplay.IsEnabled property. The following example shows code for a check box to toggle location display:

private void ShowLocationCheckBox_Click(object sender, RoutedEventArgs e)
{
    // turn location display on/off with a CheckBox
    var checkBox = sender as CheckBox;
    this.MyMapView.LocationDisplay.IsEnabled = (checkBox.IsChecked == true);
}

You may want your app to respond to new locations as they are provided. Perhaps you want to display additional information that describes the location, such as speed or estimated accuracy of the position. The following example shows information about new locations in TextBlock controls as the location is updated. MapView.SetViewAsync is then used to zoom to the location's position.

private void GetLocationChange()
{
    // get the location provider; handle the location change event
    var lp = this.MyMapView.LocationDisplay.LocationProvider;
    lp.LocationChanged += OnLocationChanged;
}

private void OnLocationChanged(object sender, Esri.ArcGISRuntime.Location.LocationInfo e)
{
    // when a new location comes in, show info to user, zoom to location
    this.HeadingTextBlock.Text = e.Course.ToString();
    this.SpeedTextBlock.Text = e.Speed.ToString();
    this.XTextBlock.Text = e.Location.X.ToString();
    this.YTextBlock.Text = e.Location.Y.ToString();
    this.MyMapView.SetViewAsync(e.Location);
}

Location display with information about the current position

Create a custom LocationProvider

LocationDisplay.LocationProvider can be set to any class that implements the ILocationProvider interface. To create your own location provider, you must implement ILocationProvider in your custom class. This interface defines the following three members:

  • LocationChanged—An event that fires when updated locations are available. The event argument is of type LocationInfo and contains information about the new location.
  • StartAsync—A method to start providing locations. As locations are read, they are exposed by raising the LocationChanged event.
  • StopAsync—A method to stop providing locations. This should cease LocationChanged events from being raised.

The following example provides a template for implementing the interface in your custom location provider class. Depending on the specifics of your custom provider, some of the details of your implementation may vary.

class MyLocationProvider : Esri.ArcGISRuntime.Location.ILocationProvider
{
    // perhaps use a flag variable to enable/disable returning locations
    private bool enableLocations;
        
    // store a reference to the current location
    private LocationInfo newLocation;

    public MyLocationProvider() 
    {
        // in the constructor, set up your internal location providing logic
        // for example:
        //     connect to a GPS device 
        //     prepare to read a file that "plays back" locations recorded in the field
        //     set up a timer that will periodically generate a random location for testing
    }

    public Task StartAsync()
    {
        // code here to start collecting locations            
        // perhaps a flag is set to enable firing the LocationChanged event
        // for example:
        this.enableLocations = true;

        return Task.FromResult<bool>(true);
    }

    public Task StopAsync()
    {
        // code here to stop collection locations
        // perhaps a flag is set to disable firing the LocationChanged event
        // for example:
        this.enableLocations = false;

        return Task.FromResult<bool>(true);
    }
        
    public event EventHandler<Esri.ArcGISRuntime.Location.LocationInfo> LocationChanged;
    private void RaiseLocationChanged()
    {
        // code to provide a location to event listeners
        // this event is raised by code that produces the new location (GPS device, e.g.)

        // perhaps a flag is used to enable/disable firing the event
        // for example:
        if (!this.enableLocations) { return; }

        if (LocationChanged != null)
        {
            // fire the LocationChanged event
            // the private "newLocation" variable is updated with a fresh location by your internal location providing logic
            LocationChanged(this, this.newLocation);
        }
    }
}

For an example of using LocationDisplay in your app and of creating a custom location provider, see the Mapping > LocationDisplay sample in the ArcGIS Runtime SDK for .NET samples. See the NmeaParser project on GitHub for an open source library for communicating with Bluetooth GPS devices.

Related topics