In a Xamarin.Forms app I'm using GeoLocator to retrieve info about my position. The method is async. I have to return the object that contains the position parameters but I can't.
This is the constructor of my class PositionPage
public PositionPage()
{
getCurrentPosition();
var map = new Map(
MapSpan.FromCenterAndRadius(
new Position(45.987487, 9.366337), Distance.FromMiles(0.3)))
{
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add(map);
Content = stack;
}
And this is the async method (Now is a void method):
public async void getCurrentPosition()
{
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
Debug.WriteLine("Position Status: {0}", position.Timestamp);
Debug.WriteLine("Position Latitude: {0}", position.Latitude);
Debug.WriteLine("Position Longitude: {0}", position.Longitude);
}
catch (Exception ex)
{
Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}
}
I need to return a position object when I call getCurrentPosition() to pass it in the MapSpan.
How can I do that?