I am using CrossGeolocator to retrieve the current latitude and longitude of the device. However I am using it inside an OnAppearing override method and it is not working. The GetPositionAsync method hangs the App.
protected override void OnAppearing()
{
base.OnAppearing();
var position = GetPosition().Result;
var lat = position.Latitude;
var lon = position.Longitude;
}
private static async Task<Position> GetPosition()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(10000);
return position;
}
Detail is that I am using this same GetPosition method in buttons in the application and works perfectly.
Could someone help me in this matter?
This worked for me.
Set up the xamarin forms maps as stated in the link. https://developer.xamarin.com/guides/xamarin-forms/user-interface/map/
set permissions as stated in below link https://jamesmontemagno.github.io/GeolocatorPlugin/GettingStarted.html
you may make use of https://jamesmontemagno.github.io/GeolocatorPlugin/CurrentLocation.html
Try this:
Create a global variable:
Then call ur method to get the position on constructor. Re-write ur method like this:
Then make a while where u want to use this:
I solved my problem just adding the parameters in GetPositionAsync: null and true.
var location = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);
and turn on the GPS
My problem was on Android 4.4
Things to take care is
calling the method in constructor. have available lat long if exists. VERY IMP have try catch, without try catch the application crashes. Not writing any thing in catch as don;t want to catch exception.
latest 2018-07-10 version update,
To support iOS 11 and earlier, you can include all three keys:
I use Xamarin to write app, and it works fine in my iOS project
reference : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/map