GetPositionAsync not work in Xamarin Android

Here is the code that I am using.
This code works on IOS but never worked on Android.
I could see some similar issues in their Project but couldn't get any help.

public async Task<Position> GetCurrentLocation()
    {
      Position position = null;
      try
      {
        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 100;

        position = await locator.GetLastKnownLocationAsync();

        if (position != null)
        {
          //got a cahched position, so let's use it.
          return;
        }

        if(!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
        {
          //not available or enabled
          return;
        }

        position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);

      }
      catch (Exception ex)
      {
        //Display error as we have timed out or can't get location.
      }

      if(position == null)
        return;

      var output = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
          position.Timestamp, position.Latitude, position.Longitude,
          position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);

      Debug.WriteLine(output);
    }

I always get task cancellation exception.

    System.Threading.Tasks.TaskCanceledException: A task was canceled.
          at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00026] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at Plugin.Geolocator.GeolocatorImplementation+<GetPositionAsync>d__33.MoveNext () [0x0042a] in C:\projects\geolocatorplugin\src\Geolocator.Plugin.Android\GeolocatorImplementation.cs:206 
        --- End of stack trace from previous location where exception was thrown ---
          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0001a] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
          at MtwPublicApp.Services.LocationService+<GetCurrentLocation>d__2.MoveNext ()

Please help me to fix
Thanks!

Answers

  • prinkpanprinkpan Member

    Hello,

    I am getting the same exception. However, observed that GetPositionAsync works when I enable the GPS on the device. Did you try that?

    Also, are you able to solve this by any other means? I am using the same code as yours.

    Thank you.

  • JohnHardmanJohnHardman GBUniversity mod

    @prinkpan @PassionateCreative - In this scenario, a TaskCanceledException is raised when the coordinates are not obtained within the timeout period. You should handle this situation in your app, as it could happen at any time. However, if you always get this exception, then you may need to enable the GPS on the device, ensure that you have the appropriate permission to access position info, and check whether other apps on the device can obtain the position using GPS. If other apps cannot obtain the GPS either, you may just need to move outside in order to do testing. Whilst I can receive GPS information inside the building I normally work in, I know of other buildings nearby where I cannot receive GPS, so make a point of testing my app in those buildings as well (I also improvise a Faraday cage for testing whilst connected to the debugger).

  • gioxgiox Member ✭✭

    @PassionateCreative I'm a beginner, could you please tell me how do you call the GetCurrentLocation() async method waiting for the GPS position?

Sign In or Register to comment.