简体   繁体   中英

How can I get GPS Location of my device in Xamarin Forms?

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");
        }
    }

I am also working on my first Xamarin project that requires location permissions. A couple of things you should try that may help you get GPS location.

My suggestions

1) You need to grant additional permissions in AndroidManifest file. These are

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

2) Since Android 6.0 or above , you need to have runtime permissions . To acchieve that, you need to override OnStart() method in your MainActivity.cs . I referenced to Location permission for Android above 6.0 with Xamarin.Forms.Maps . Here is what I did

protected override void OnStart()
{
    base.OnStart();
    if(CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
    {
        RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
    }

}

Important: My solution here is just temporarily handle granting permissions. Visual Studio would show an exception when you build and run the project first time. After you choose "allow" from "Allow access location?"prompt on the device, you would not get the exception when run the project again. I have not handled this issue yet.

My limits

As I said, the problem isn't completely solved. My answer is to help you temporarily solve location permissions issue and move on. And I don't know how to get location offline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM