Get Device Current Location In Windows 10 UWP

In device information we can get the Latitude and Longitude of the current location which the user needs.

To access the location information in Windows 10 application we need to accessit using the GeoLocator class which has the property to get us the information of Latitude and Longitude of the specific location.

Let’s see the steps.

Create Windows 10 application and enable the “Location” capabilities for the application to access the location. For that open the Package.appxmanifest file and navigate to the Capabilities tab then enable Location capability as in the following screenshot,

location

Now go to the code behindthe page. Create an instance of Geolocator class; with the instance you can get the desired location by accessing the GetGeopositionAsync() method to get the device location information.

C# Code

  1. private async void getBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     var geoLocator = new Geolocator();  
  4.     geoLocator.DesiredAccuracy = PositionAccuracy.High;  
  5.     Geopositionpos = await geoLocator.GetGeopositionAsync();  
  6.     string latitude = "Latitude: " + pos.Coordinate.Point.Position.Latitude.ToString();  
  7.     string longitude = "Longitude: " + pos.Coordinate.Point.Position.Longitude.ToString();  
Now run the app and see the expected output like the following screen.

Firstly, it will ask user permission to access the device location.

location
location


For more information on Windows 10 UWP, refer my e-book:
Read more articles on Universal Windows Platform:


Similar Articles