I want to get the Longitude and Latitude of my device when the entry box named entLocation is focused. I am using Xamarin.Essential Geolocation to get the GPS location of my device I followed the documentation and tutorials and still I am unable to get the GPS Location. I have added permission below to my android manifest still nothing. I am getting an display alert on "Error3", Error3 is the exception when Unable to get location.
The error is "Java.Lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.requestPermissions(java.lang.String[],int)' on a null object reference"
My questions:
1. What is my error in my code?
2. Can I get my gps location offline? If yes, how can I achieve that?
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-feature android:name="android.hardware.location" android:required="false"
uses-feature android:name="android.hardware.location.gps" android:required="false"
uses-feature android:name="android.hardware.location.network" android:required="false"
private async void entLocation_FocusedAsync(object sender, FocusEventArgs e)
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
entLocation.Text = location.Longitude.ToString() + "," + location.Latitude.ToString();
}
}
catch (FeatureNotSupportedException fnsEx)
{
await DisplayAlert("Error1", fnsEx.ToString(), "ok");
}
catch (PermissionException pEx)
{
await DisplayAlert("Error2", pEx.ToString(), "ok");
}
catch (Exception ex)
{
await DisplayAlert("Error3", ex.ToString(), "ok");
}
}