Advanced UWP: Geolocation Services | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (182).NET Core  (28).NET MAUI  (233)Angular  (117)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (42)Black Friday Deal  (1)Blazor  (244)BoldSign  (15)DocIO  (24)Essential JS 2  (110)Essential Studio  (201)File Formats  (74)Flutter  (140)JavaScript  (227)Microsoft  (122)PDF  (86)Python  (1)React  (107)Streamlit  (1)Succinctly series  (131)Syncfusion  (993)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (165)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (11)Business intelligence  (55)Button  (4)C#  (164)Chart  (154)Chart of the week  (62)Cloud  (16)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (74)Development  (694)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (31)File Manager  (8)Gantt  (23)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (530)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (48)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (426)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (648)What's new  (354)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Advanced UWP: Geolocation Services

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.

About Advanced UWP Part 1: Interacting with the Real World

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.


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed