The following excerpt is from Advanced UWP Part 1: Interacting with the Real World, the first in a five-part series written by Succinctly series author Matteo Pagani. To download the complete white paper, and other papers in the series, visit the White Paper section of Syncfusion’s Technology Resource Portal.
The geolocation services offered by the Universal Windows Platform can be used to track a user’s position in the world, no matter which device the app is running on. Windows can determine the current position using different approaches: by using a GPS sensor, by getting the position from the nearest mobile cell (if the device has a SIM), or based on the IP returned by the Wi-Fi connection. The approach leveraged by the system is transparent to the user. The Universal Windows Platform will take care of choosing the best one for us according to the requirements and to the hardware configuration of the device.
Note: To use the geolocation service, you need to enable the Location capability in the manifest file.
The main class we’re going to use to interact with the geolocation services is called Geolocator, which is part of the Windows.Devices.Geolocation namespace.
Retrieving the status of the services
The availability of geolocations services can’t be taken for granted. The user can decide not to give permission to our application to access to the user’s location.
This scenario is automatically handled by Windows. The first time we use the geolocation APIs, Windows will ask the user if they want to give the app access to location services. However, as developers, the best approach is to manually check if we have received the proper permission by leveraging a new static method, introduced in Windows 10 and offered by the Geolocator class, called RequestAccessAsync():
When you call it, the user will be prompted with the following message:
Figure 1: The app request to access to the user’s location.
Only if the user has chosen Yes will you receive in return the value Allowed of the enumerator GeolocationAccessStatus. It’s important to highlight that this prompt will appear only the first time. If you call the RequestAccessAsync() method again, but the user has already granted the permission, you will automatically get Allowed and Windows won’t show the pop-up again.
However, it’s good practice to always check the status of the request before using the location APIs. The users can, at any time, go into Windows settings and, in the Privacy section, disable the location access to the app, even if they have previously granted it. Then you’ll start to get Denied as the value of the GeolocationAccessStatus enumerator.
Retrieving the user’s position
The simplest way to determine the user’s position is to use the GetGeopositionAsync() method exposed by the Geolocator class, which performs a single acquisition. The method returns a Geoposition object containing all the information about the current location inside the Point property. For example, this property exposes an object called Position, which you can use to retrieve information like Latitude, Longitude, and Altitude.
Another approach is to subscribe to an event called PositionChanged. This way, we can track the user’s position and be notified every time they change position. We can customize the frequency of the notification with two parameters: MovementThreshold (the distance, in meters, that should be traveled from the previous point) and ReportInterval (the time-frame, in milliseconds, that should pass between one notification and the other). Here is a sample code to continuously track the user’s position:
The event handler we created to handle the PositionChanged event contains a parameter with a property called Position. It’s a Geoposition object with the coordinates of the current user’s position. Please note that we’re using the Dispatcher to display this data in the page: it’s required, since the PositionChanged event is managed in a background thread, while the controls placed in the page are handled by the UI thread.
Checking the status of the location services
The Geolocator class also offers an event called StatusChanged, which is triggered when the status of the services changes. This is another reason not to take for granted the availability of location services. The users can find themselves in areas with no GPS coverage or no Internet connection and, consequently, the device wouldn’t be able to retrieve the current location.
By subscribing to this event and by checking the value of the LocationStatus property, you’ll get in return an enumerator of type PositionStatus, which contains different values based on the location services’ status.
The supported values are:
- Ready: the location platform is active and it’s returning valid data.
- Initializing: the location platform is attempting to localize the user.
- NoData: the location platform can’t retrieve the current position.
- Disabled: access to the location platform is disabled.
- NotInitialized: the location platform hasn’t tried to localize the user yet.
- NotAvailable: the location platform isn’t available on the current device.
By reading Matteo Pagani’s white paper on UWP, you will understand how to interact with the real world in a Windows 10 application developed with the Universal Windows Platform. You will learn how to use the location services to identify the position of a user, display it on a map, and provide advanced interactions like displaying a point of interest or a route on a map; how to understand when a user enters or exits in a specific location; how to work with the motion sensors that many devices (especially the portable ones) have to offer, like accelerometers, gyroscopes, compasses; and more.