Working With Geo Location Windows Phone 8.1

You have the GeoLocator class in the Windows.Devices.GeoLocation namespace.

GeoLocator 

As a developer the first thing you need to be sure of is that the location capabilities in the Package.appxmanifest are enabled as shown below.

appxmanifest

The following is a sample code to fetch the Location information.

  1. private Geolocator _geolocator = null;    
  2. async private void GetGeolocation()     
  3. {       
  4.     _geolocator = new Geolocator();     
  5.     // Desired Accuracy needs to be set     
  6.     // before polling for desired accuracy.     
  7.     _geolocator.DesiredAccuracyInMeters = 50;     
  8.     try     
  9.     {     
  10.        // Carry out the operation  Geoposition pos =    
  11.        await   _geolocator.GetGeopositionAsync();     
  12.     }      
  13.     catch      
  14.     {     
  15.        /*Operation aborted Your App does not have permission to access location     data.  Make sure you have defined ID_CAP_LOCATION in the    application manifest and that on your phone,  you have turned on location by checking Settings > Location.*/      
  16.        //if the location service is turned off then you can take the user to the   location setting with the below code      
  17.       await Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));     
  18.     }    
  19. }  
You can set the timeout period to fetch the location information and also you can set the pool time interval before which the location API is called again.
  1. Geoposition pos = await _geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(15));   
The preceding piece of code says that if a request is made then get the location information and then the system will check the time interval since the last request was made. If the request is made within 10 minutes then the last cached value will be returned, if not then he knows the values will be fetched and also the API will timeout if the request does not respond within 15 minutes of the request.

How to Cancel a request made to get the Geo location information

If you have requested location information and for some reason you want to cancel the request then all you need to do is to use the CancellationToken Object in conjunction with the GeoLocator object as shown below.
  1. private CancellationTokenSource _cts = null;     
  2. async private void GetGeolocation()     
  3. {
  4.     _geolocator = new Geolocator();     
  5.      _cts = new CancellationTokenSource();    
  6.      CancellationToken token = _cts.Token;     
  7.      // Desired Accuracy needs to be set     
  8.      // before polling for desired accuracy.    
  9.      _geolocator.DesiredAccuracyInMeters = 50;     
  10.    try     
  11.    {       
  12.        // Carry out the operation   Geoposition pos = await _geolocator.GetGeopositionAsync().AsTask(token);     
  13.    }     
  14.    catch     
  15.   {     
  16.       /*Operation aborted Your App does not have permission to access location data.   Make sure you have defined ID_CAP_LOCATION in the application manifest and that on your phone,   you have turned on location by checking Settings > Location.*/      
  17.     //if the location service is turned off then you can take the user to the location setting with the below code     
  18.      await Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));  }    
  19.   }     
  20.  //Call this Method to cancel the current location access request cancelGelocationRequest()    
  21.   {     
  22.      if (_cts != null)    
  23.        {  _cts.Cancel();     
  24.     _cts = null;    
  25.   }    
  26. }   
How to Track Location Changes

If you want real-time info as soon as the user location changes then you need to subscribe to the StatusChanged and PositionChanged events of the GeoLocator object.

The following is the code snippet for that:
  1. private Geolocator _geolocator = new Geolocator();     
  2. TrackMyLocation()    
  3. {     
  4.     _geolocator.StatusChanged += _geolocator_StatusChanged;  _geolocator.PositionChanged += _geolocator_PositionChanged;    
  5. }    
  6.     
  7. void _geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)     
  8. {    
  9.     // will get a new GeoPostion object in the args paramter which gives the updated lat long values     
  10. }    
  11.     
  12. void _geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)     
  13. {  //gives s postionstatus object with below values     
  14.     /* Ready   
  15.     Initilizing  
  16.      NoData    
  17.     Disabled   
  18.     NotInitilized   
  19.     NotAvalible  */    
  20.  }    

Up Next
    Ebook Download
    View all
    Learn
    View all