public class GetCurrentLocationAndroid : IGetCurrentLocation
{
///
/// Instance of the Geoloactor
///
private Geolocator _locator;
///
/// The get current location.
///
///
/// The .
///
public async Task GetCurrentLocation()
{
System.EventHandler handler = null;
var result = new LocationCoordinates();
var tcs = new TaskCompletionSource();
this._locator = new Geolocator(Forms.Context) { DesiredAccuracy = 10 };
try
{
if (!this._locator.IsListening)
{
this._locator.StartListening(1, 1);
}
handler = async (object sender, PositionEventArgs e) =>
{
result.Status = e.Position.Timestamp;
result.Latitude = e.Position.Latitude;
result.Longitude = e.Position.Longitude;
result.Accuracy = e.Position.Accuracy;
_locator.PositionChanged -= handler;
tcs.SetResult(result);
};
this._locator.PositionChanged += handler;
await this._locator.GetPositionAsync(timeout: 10000).ContinueWith(
t =>
{
});
}
catch (System.Exception ex)
{
if (ex.InnerException.GetType().ToString() == "Xamarin.Geolocation.GeolocationException")
{
tcs.SetException(ex);
}
else
{
tcs.SetException(ex);
}
}
return tcs.Task.Result;
}
}