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

loot verge

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get the current device model on Xamarin Forms?

From Dev

How can I get the current GPS location?

From Dev

How to get my Android device GPS location via Here API?

From Dev

How can i get current GPS location ? (not lastknown)

From Dev

How can I get accurate latitude/longitude of current GPS location?

From Dev

Get device location in latitude-longitude in xamarin forms

From Dev

How do I get a GPS location update synchronously in Android using Xamarin?

From Dev

Given a list of GPS coordinates making up a boundary, how can I calculate if my location is within that boundary?

From Dev

How do I get the data from a serial GPS connected to my android device?

From Dev

My GPS provider is enabled but I get null location nonetheless

From Dev

How can I get sensors values with xamarin forms?

From Dev

How can I get my location with Kotlin dev Android?

From Dev

How can i get my location google image?

From Dev

How can i get user nearby my location in geofire,Firebase

From Dev

How can I debug Visual Studio - Xamarin.Forms project on Android device?

From Dev

How can I call this async method in my Xamarin Forms when my app starts?

From Dev

How can I call this async method in my Xamarin Forms when my app starts?

From Dev

How to get device serial number of an iOS using Xamarin forms

From Dev

How to get device location

From Java

With Xamarin Forms, how can I create my SQLite connections in shared code?

From Dev

How can I save some user data locally on my Xamarin Forms app?

From Dev

How can I link a CarouselView and Indicator when using C# markup in my Xamarin forms app?

From Dev

Can I get location of android device without GPRS on server side?

From Dev

How can I whitelist my Android device?

From Dev

Why can't I add a earlier xamarin forms version as a nuget into my Xamarin forms project/android package?

From Dev

How can I get the coordinates when user tap on a Map in Xamarin.Forms?

From Dev

How can I see distance between two pins in xamarin forms and get the distance as a string?

From Dev

How can I return the value from my Task<string> in my iOS Dependecy Service [Xamarin.Forms.iOS]?

From Dev

Get correct device width in Xamarin forms

Related Related

  1. 1

    How can I get the current device model on Xamarin Forms?

  2. 2

    How can I get the current GPS location?

  3. 3

    How to get my Android device GPS location via Here API?

  4. 4

    How can i get current GPS location ? (not lastknown)

  5. 5

    How can I get accurate latitude/longitude of current GPS location?

  6. 6

    Get device location in latitude-longitude in xamarin forms

  7. 7

    How do I get a GPS location update synchronously in Android using Xamarin?

  8. 8

    Given a list of GPS coordinates making up a boundary, how can I calculate if my location is within that boundary?

  9. 9

    How do I get the data from a serial GPS connected to my android device?

  10. 10

    My GPS provider is enabled but I get null location nonetheless

  11. 11

    How can I get sensors values with xamarin forms?

  12. 12

    How can I get my location with Kotlin dev Android?

  13. 13

    How can i get my location google image?

  14. 14

    How can i get user nearby my location in geofire,Firebase

  15. 15

    How can I debug Visual Studio - Xamarin.Forms project on Android device?

  16. 16

    How can I call this async method in my Xamarin Forms when my app starts?

  17. 17

    How can I call this async method in my Xamarin Forms when my app starts?

  18. 18

    How to get device serial number of an iOS using Xamarin forms

  19. 19

    How to get device location

  20. 20

    With Xamarin Forms, how can I create my SQLite connections in shared code?

  21. 21

    How can I save some user data locally on my Xamarin Forms app?

  22. 22

    How can I link a CarouselView and Indicator when using C# markup in my Xamarin forms app?

  23. 23

    Can I get location of android device without GPRS on server side?

  24. 24

    How can I whitelist my Android device?

  25. 25

    Why can't I add a earlier xamarin forms version as a nuget into my Xamarin forms project/android package?

  26. 26

    How can I get the coordinates when user tap on a Map in Xamarin.Forms?

  27. 27

    How can I see distance between two pins in xamarin forms and get the distance as a string?

  28. 28

    How can I return the value from my Task<string> in my iOS Dependecy Service [Xamarin.Forms.iOS]?

  29. 29

    Get correct device width in Xamarin forms

HotTag

Archive