Get User Location In UWP

As you well know new generation of apps interact with user more.I personally call them "clever-apps".Clever apps gets information from you and at background they provide with much content regarding the information given.
 
Some informations can be text based,images and many more including Location. Location-based apps are on market for a couple of years now and it shows they will still be around.Location is perfect instrument to access users spot in map and provide the user with content.The content can be nearest "things",friendship like Happn,networking like Swarm or e-commerce products.
 
In UWP apps,we access users location easily.

Here,I will be showing you how to achive this:

Note: Before starting make sure you have set permissions for accessing location in your manifest file.

You can open Manifest XML file and add that capabilities:
  1. <Capabilities>      
  2.     <DeviceCapability Name="location"/>  
  3.   </Capabilities>  
Or you can open Manifest in Designer View,get to Capabilities section and tick "Location" capability.

Lets write some to access it!

First of all,I'd like to mention that you don't get location directly without asking user!

You request it:
  1. var access = await Geolocator.RequestAccessAsync();  
You won't be getting location information until user accepts it.And it shows only once,so if you accept it,next run the app won't be asking you again.

Next,you need to use cases to see if access is allowed:
  1. switch (access)  
  2. {  
  3.     case GeolocationAccessStatus.Allowed:  
  4.         Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };  
  5.         geolocator.StatusChanged += OnStatusChanged;  
  6.         Geoposition pos = await geolocator.GetGeopositionAsync();  
  7.         break;  
  8.   
  9.     case GeolocationAccessStatus.Denied:  
  10.      //You can show a message here if user declined.  
  11.     break;  
  12. }  
Here we control 2 cases whether its allowed or not.If allowed,we'll be getting information.

You should also check if location status changes due to disconnectivity.
  1. async private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)  
  2. {  
  3.     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>  
  4.     {          
  5.         switch (e.Status)  
  6.         {  
  7.             case PositionStatus.Ready:  
  8.                //If its ready to get location data,you can add some code.  
  9.                 break;  
  10.   
  11.             case PositionStatus.Initializing:  
  12.                 //Location is being initialized.waiting for it to complete.  
  13.                 break;  
  14.   
  15.             case PositionStatus.NoData:  
  16.                 //Some places can not access location.Metros,Mountains,Elevators or fields with jammers.This case works when you're in one of them.  
  17.                 break;  
  18.   
  19.             case PositionStatus.Disabled:  
  20.                //You either rejected location access at start or closed Location.  
  21.                 break;  
  22.   
  23.             case PositionStatus.NotInitialized:  
  24.                 //The app has not yet accessed location data.  
  25.                 break;  
  26.   
  27.             case PositionStatus.NotAvailable:  
  28.                 //Location may not be possible due to OS settings.  
  29.                 break;  
  30.   
  31.             default:  
  32.                //If non of above works,this will.Writing a message helps.  
  33.                 break;  
  34.         }  
  35.     });  
  36. }  
As a last job,you need to get the Latitude and Longitude information from GeoPosition object:
  1. //Define an object named Lat to access Latitude.  
  2. Lat = (float)pos.Coordinate.Point.Position.Latitude;  
  3.   
  4. //Define an object named Lon to access Longitude  
  5. Lon = (float)pos.Coordinate.Point.Position.Longitude;  
 Thats all for accessing user location. Latitude and Longitude is all you need for getting users location.  

Up Next
    Ebook Download
    View all
    Learn
    View all
    Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.