I am working on a check-in project which requests location access. I am using Xamarin.Essentials to get user location.
The general work flow is: granted location permission -> get location -> do check in transaction
The only issue is, on ios, when a new user admits the request at the first time, the app seems not receiving the location result immediately, which results in failing to do the following transaction.
But If users click check-in button again, it manages to receive the location and check in.
The same code is working smoothly on Android.
Here is part of the source code:
In the view model:
private ICommand CheckInCommand => new Command(async () => { try { var currentLocation = await _geolocationHelper.GetLocation(); // didn't go through the following code after user admits the accessing location request at the first time use. // the following is to make check in transaction var clockInDateTime = DateTime.UtcNow; var currentAddress = await _geolocationHelper.GetAddress(currentLocation.Latitude, currentLocation.Longitude); ....... }
GeolocationHelper:
public async Task<Location> GetLocation() { try { var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(5)); var location = await Geolocation.GetLocationAsync(request); if (location == null) return new Location(0, 0); return location; } catch (PermissionException pEx) { // Handle permission exception throw new GeolocationPermissionError(pEx.Message); } catch (Exception exp) { throw new GeolocationError(exp.Message); } }
Anything could be missing or incompatible? I am wondering whether there is a threading issue. But if so, how come it is working well on Android?
Answers
Have you tried to adjust the accuracy to:
It may spend some time to do a full query for the first time. Have you tried to add a breakpoint there to see whether the application is going on?
The bug happens in Xamarin.Essential and I installed the 1.6pre version which works perfectly now.