Showing locations

If you are making a mapping app these days without providing the user the option to show their location, you've kind of missed the 21st century boat, so to speak. For years now, this has been a standard feature of Google and Apple's mapping technology. ArcGIS Runtime comes with everything you will need to display your current location and more:

Showing locations

Displays the current location

Let's look at the classes that you will need to use. First, LocationDisplay is a property of MapView. It contains everything you need to show the current location. Here's an example of how it is done:

<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>
<Grid>
    <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>
</Grid>

As you can see, the LocationDisplay object is instantiated in this XAML code, and then the properties of LocationDisplay are set. These properties include AutoPanMode, DefaultSymbol, IsEnabled, and IsPingAnimationEnabled. The most important property is IsEnabled. This property tells the LocationDisplay object to start showing your location. However, in order for this to work on a desktop or laptop, you will need to install an external GPS. If your mobile device has a built-in GPS, this will, generally speaking, work as shown previously, without having to do anything else. If you have an external GPS, you will need to install the drivers and software and write some more code to translate the incoming coordinates into something ArcGIS Runtime can use.

The AutoPanMode method includes three options: Off, Default, and Navigation. The Off option indicates that the map does not move as the device moves. The Default option re-centers the map to the current location. Lastly, Navigation changes the heading and position of the map as the location and heading changes for the device. The CourseSymbol instance is the current location when moving, while DefaultSymbol is the location when stationary. The IsPingAnimation property shows an animation as new locations are received.

Another important class is LocationInfo, which provides you with a set of events to handle as your location changes. The LocationChanged event is used to get new locations as they come from the provider (GPS). With this class, you can get the course, horizontal accuracy, location, and speed. This would be easy to implement using the MVVM pattern with a behavior by handling the LocationChanged event, as shown here:

AssociatedObject.LocationDisplay.LocationProvider.LocationChanged +=  
    LocationProvider_LocationChanged;

Then, OnLocationChanged can be handled:

void LocationProvider_LocationChanged(object sender, 
    Esri.ArcGISRuntime.Location.LocationInfo e)
{
    this.heading = e.Course.ToString();
    this.speed = e.Speed.ToString();
    this.x = e.Location.X.ToString();
    this.y = e.Location.Y.ToString();
    this.mapView.SetViewAsync(e.Location);
}

If you need to use an external GPS, you may need to implement your own LocationProvider class by implementing ILocationProvider. For example, many GPS support the National Marine Electronics Associated (NMEA) specification. This specification allows computers to send information between a GPS and a computer. Navigate to http://www.nmea.org/ for more information. Esri has implemented a library on GitHub that allows you to interface with an NMEA compliant device so that you don't have to implement this interface yourself. Navigate to https://github.com/dotmorten/nmeaparser. For your convenience, the DLL has been placed under C:ArcGISRuntimeBookNmeaParser.WinDesktop.dll. Once you've added the DLL to your project, you will need to create a class like the following code:

public class NmeaLocationProvider : ILocationProvider
{
    public event EventHandler<Esri.ArcGISRuntime.Location.LocationInfo> LocationChanged;
    private NmeaParser.NmeaDevice device;

    public NmeaLocationProvider(NmeaParser.NmeaDevice device)
    {
        this.device = device;
        device.MessageReceived += device_MessageReceived;
    }

    void device_MessageReceived(object sender, NmeaParser.NmeaMessageReceivedEventArgs e)
    {
        var message = e.Message;
        if (message is NmeaParser.Nmea.Gps.Gprmc)
        {
            var rmc = (NmeaParser.Nmea.Gps.Gprmc)message;
            if (rmc.Active && LocationChanged != null)
            {
                LocationChanged(this, new 
                    Esri.ArcGISRuntime.Location.LocationInfo()
                {
                    Course = rmc.Course,
                    Speed = rmc.Speed,
                    Location = new 
                    Esri.ArcGISRuntime.Geometry.MapPoint(
                        rmc.Longitude, 
                        rmc.Latitude, 
                        SpatialReferences.Wgs84)
                });
            }
        }
    }

    public System.Threading.Tasks.Task StartAsync()
    {
        return this.device.OpenAsync();
    }

    public System.Threading.Tasks.Task StopAsync()
    {
        return this.device.CloseAsync();
    }
}

This class accepts the device (NmeaDevice), and then handles incoming location events via device_MessageReceived. When the message with the location arrives, it is parsed and the new location is calculated. The StartAsync method is called once this class is instantiated. You will then need to instantiate this custom provider using the following code:

string comport = "COM3";
int baudRate = 4800;
var port = new System.IO.Ports.SerialPort(commPort, baudRate);
    var device = new NmeaParser.SerialPortDevice(port);
    mapView.LocationDisplay.LocationProvider 
    = new NmeaLocationProvider(device);

After NmeaLocationProvider is instantiated, you just need to set mapViewLocationDisplay.IsEnabled = true and your external GPS will send in its location and show up on the map.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.4.191