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

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

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.

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.

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);
}
}
}