Issue
I have a class in which I have a Timer and at Intervals of 15 seconds I try to get the GeoLocation.
private System.Timers.Timer _getLocationTimer { get; set; }
_getLocationTimer = new System.Timers.Timer { Interval = 15000 };
_getLocationTimer.Elapsed -= GetLocation_Handler;
_getLocationTimer.Elapsed += GetLocation_Handler;
_getLocationTimer.Start();
async void GetLocation_Handler(object sender, ElapsedEventArgs e)
{
Position position = null;
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100;
position = await locator.GetLastKnownLocationAsync();
if (position != null)
{
_position = new Position(position.Latitude, position.Longitude);
//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(10));
}
catch (Exception ex)
{
throw ex;
return;
}
_position = new Position(position.Latitude, position.Longitude);
if (position == null)
return;
}
My problem is with method GetPositionAsync because sometimes and only one some phones it does not return (just hangs in the method). I am using Xam.Plugin.Geolocator library from nuget. Can someone please tell me what can I do so that GetPositionAsync can work each time?
Solution
For now, Xamarin.Essentials make the request of permission easier. Update the Xamarin.Essentials to the latest version. You only need to add permissions in the manifest file of android and the Info.plist file of iOS.
For more details for the Geolocation permissions, check the link below. https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android
Xamarin.Essentials: Permissions: https://docs.microsoft.com/en-us/xamarin/essentials/permissions?context=xamarin%2Fxamarin-forms&tabs=android
Answered By - Wendy Zang - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.