To get the phone's current geo-location, altitude, speed, .. you can use the following:
if (await Device.Location.IsSupported())
{
await Alert.Show("Geo location is not supported on your device.");
return;
}
if (!await Device.Location.IsEnabled())
{
await Alert.Show("Geo location is not enabled on your device.");
return;
}
// Prompt the user to ask for permissoin (unless permission is already granted)
if (await DevicePermission.Location.Request() != PermissionResult.Granted)
{
await Alert.Show("Permission was not granted to access your current location.");
return;
}
var position = await Device.Location.GetCurrentPositionSilently(desiredAccuracy, timeout);
If location is not available for any reason, then NULL will be returned.
So you should handle that:
{
await Alert.Toast("Your location is not available. Please try again later.");
return;
}
else
{
// Now you can use the position data
}
The above scenario is quite common. So instead of writing all of the above code yourself, you can just call the RequestOrGetCurrentPosition() method.
if (position != null)
{
// Just use it. All due error messages will have been shown.
}
The result is an instance of the following class:
{
// Always provided:
public double Latitude;
public double Longitude;
public double Accuracy;
// May or may not be provided.
public double? Altitude;
public double? AltitudeAccuracy;
public double? Speed;
}
As you can see the if you want to use Altitude and Speed properties, your code should handle the scenario of them not being returned.