Advanced UWP: Geolocation Services | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (172).NET Core  (29).NET MAUI  (192)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (209)BoldSign  (12)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (131)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (882)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (49)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (125)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (613)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)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  (488)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (41)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (368)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (30)Visual Studio Code  (17)Web  (577)What's new  (313)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.

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top