Xamarin Forms Labs's Geolocator.GetPositionAsync always throws TaskCanceledException (iOS)

NamHoang.3859NamHoang.3859 CAMember ✭✭
edited May 2015 in Xamarin.Forms

I am using XFLabs.Geolocator (https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator) in a Xamarin Forms PCL project. There is no published Nuget package for XFLabs in iOS and Android. Therefore, I read the code from Github and copy it to my PCL and iOS projects.

            var geolocator = DependencyService.Get<IGeolocator>();
//            geolocator.StartListening(1000, 1);
            var position = geolocator.GetPositionAsync(10000).Result; // THIS LINE HAS PROBLEMS
            Debug.WriteLine(position.Longitude);
            Debug.WriteLine(position.Latitude);

The DependencyService works fine. I got back a geolocator like this

{MyProject.iOS.Geolocator}
    DesiredAccuracy: 0
    IsGeolocationAvailable: true
    IsGeolocationEnabled: false  // This is FALSE, is it a problem?
    IsListening: false
    SupportsHeading: false

However, the position assignment line always throw a TaskCanceledException. If I uncomment the geolocator.StartListening line, the position assignment line just run forever. Fyi, I have added NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription to my info.plist. Furthermore, the application never asks me for Location Permission.

Can you please help me with this problem.

Thanks so much!

Posts

  • NamHoang.3859NamHoang.3859 CAMember ✭✭
    edited May 2015

    UPDATE 1:

    I can make the app ask for Permission from a pop-up now. I just put the code block inside a Button.Clicked event in order to make the block executes after the ViewDidLoad() function. After pushing the Allow button on the pop-up, the geolocator looks like this:

    {MyProject.iOS.Geolocator}
        DesiredAccuracy: 0
        IsGeolocationAvailable: true
        IsGeolocationEnabled: true  // This has changed to TRUE
        IsListening: false
        SupportsHeading: false
    

    However, the problem still persists.

  • Bikash.4523Bikash.4523 USMember
    edited May 2015

    "In FinishedLaunching write
    SetIoc ();
    DependencyService.Register < Geolocator > ();

    private void SetIoc ()
    {
    var resolverContainer = new SimpleContainer ();
    resolverContainer.Register < IDevice > (t => AppleDevice.CurrentDevice)
    .Register < IDisplay > (t => t.Resolve < IDevice > ().Display)
    .Register < IDependencyContainer > (t => resolverContainer);

            Resolver.SetResolver (resolverContainer.GetResolver ());
        }"
    
  • NamHoang.3859NamHoang.3859 CAMember ✭✭
    edited May 2015

    Thank you for your answer. However, I don't think setting IOC will solve the problem because the DependencyService in my code works fine, I have already got back the Geolocator by using [assembly: Dependency(typeof(MyProject.iOS.Geolocator))]

  • NamHoang.3859NamHoang.3859 CAMember ✭✭

    I have solved the problem. I shouldn't use .Result in the Async call. The task actually operates on UI thread so using .Result will block it. I use await now and it works fine.

  • samthui7samthui7 VNMember

    @NamHoang.3859 said:
    I have solved the problem. I shouldn't use .Result in the Async call. The task actually operates on UI thread so using .Result will block it. I use await now and it works fine.

    Can you post your final code?

    I used async/await too, but the exception still be thrown.
    Only after adding try/catch, the problem gone away.

  • FelipeSouzaLongoFelipeSouzaLongo USMember ✭✭

    @NamHoang.3859 said:
    I have solved the problem. I shouldn't use .Result in the Async call. The task actually operates on UI thread so using .Result will block it. I use await now and it works fine.

    Can you post your final code please ?

Sign In or Register to comment.