Xamarin form Geolocation task cancelled exception

后端 未结 4 1680
旧时难觅i
旧时难觅i 2021-01-16 05:01

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

4条回答
  •  滥情空心
    2021-01-16 05:15

    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;
                    }
                }
            }
    

提交回复
热议问题