1

I am working on Xamarin form app with andorid, UWP and Windows 8 project. I am using Geolocation plugin created by Jamesmontemagno to get the current device location. It is working fine in windows 8 and UWP but whenever I am trying to run it against the android device I keep getting task cancelled exception. I have checked all the permissions that are required as per suggestion but still no luck. My code to access location is below

  protected override void OnAppearing()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100; //100 is new default
            if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
            {
                try
                {
                    var position = locator.GetPositionAsync(timeoutMilliseconds: 60000).Result;
                    //var pp = helper.Setting.Location;
                    var Latitude = position.Latitude;
                    var Longitude = position.Longitude;
                }
                catch(Exception ex)
                {
                    var exc = ex;
                }
            }
        }

Below is an image for my settings for android manifest enter image description here

Sjoerd Pottuit
  • 2,307
  • 4
  • 20
  • 35
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
  • have you tried using the await keyword? like here: https://github.com/jamesmontemagno/GeolocatorPlugin – Radinator Sep 26 '16 at 13:18
  • What device are you testing on? There are issues with some android devices like Meizu, that causes this exception. Try rebooting your phone – Artem Zelinskiy Sep 26 '16 at 13:32
  • @Radinator I have been working with Tasks for so long but never thought that await would fix it got it working finally please change your comment to an answer so that I can accept it I will post the working solution also – Atul Chaudhary Sep 26 '16 at 13:34
  • @Greensy Thanks for reply I have just replied to Radiator suggestion and it turn out to be that if I am using await that works instead or Task.Result – Atul Chaudhary Sep 26 '16 at 13:35

4 Answers4

3

Try using the await keyword like it is used in the original code:

try
{
  var locator = CrossGeolocator.Current;
  locator.DesiredAccuracy = 50;

  var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);

  Console.WriteLine ("Position Status: {0}", position.Timestamp);
  Console.WriteLine ("Position Latitude: {0}", position.Latitude);
  Console.WriteLine ("Position Longitude: {0}", position.Longitude);
}
catch(Exception ex)
{
  Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}

This should take care that there are no race condition and therefore TaskCancellationException.

Sjoerd Pottuit
  • 2,307
  • 4
  • 20
  • 35
Radinator
  • 1,048
  • 18
  • 58
3

For anyone else who gets a timeout even with the await, only on Android, and even though the device's Google Maps app works fine, you are probably running into this bug which only happens on certain Android devices, but quite a few of them at that.

The issue is an old one that Google has never fixed. The solution, and one possible reason the Google Maps app works fine, is to use Google Play Services' fused location provider.

Currently the Geolocator Plugin just uses the regular Android Location Provider, but James has mentioned that he would like to use the Fused provider at some point. I have yet to try the fused provider myself though.

hvaughan3
  • 10,955
  • 5
  • 56
  • 76
1

Thanks to @Radinator below is the working solution.

protected async override void OnAppearing()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100; //100 is new default
            if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
            {
                try
                {
                    await SetLocation();
                }
                catch (Exception ex)
                {
                    var exc = ex;
                }
            }
        }

        private async Task SetLocation()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100; //100 is new default
            if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
            {
                try
                {
                    var position = await locator.GetPositionAsync(timeoutMilliseconds: 60000);

                    var Latitude = position.Latitude;
                    var Longitude = position.Longitude;
                }
                catch (Exception ex)
                {
                    //log ex;
                    throw ex;
                }
            }
        }
Sjoerd Pottuit
  • 2,307
  • 4
  • 20
  • 35
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
0

Faced 'Task killed' issue with v3.0.4. The following worked for me:

  1. Uninstall the app
  2. Update Geolocator to prerelease 4.0
MJH
  • 2,301
  • 7
  • 18
  • 20