dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

[Bug] MAUI Essentials GetLocationAsync doesn't work - throws error #1166

Closed naweed closed 2 years ago

naweed commented 3 years ago

Description

I am not able to use GetLocationAsync in MAUI Essentials. It throws an error that "The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class."

Steps to Reproduce

Run the following code:

        //Try to get My Current Location                    
        try
        {
            location = await Geolocation.GetLocationAsync();
        }
        catch (Exception ex)
        {
            // Unable to get location
            // Default to Bahrain location
            location = new Location(26.20397160522508, 50.56526549999999);
        }

Expected Behavior

The app should launch and ask for location permissions.

Actual Behavior

It goes into the exception block with the following error: "System.NullReferenceException: The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class. at Microsoft.Maui.Essentials.Platform.GetCurrentActivity(Boolean throwOnNull)"

Basic Information

Other Info

Testing against a physical device (Samsung S20+)

ghost commented 3 years ago

I ran into the same issue. I resolved it by following the docs on https://docs.microsoft.com/en-us/xamarin/essentials/geolocation but using Maui Essentials instead of Xamarin.

After modifying the MainActivity.cs within the Android platform folder it asks me for location permission.

Edit: I have not tested this for iOS yet.

VincentBu commented 2 years ago

repro on Android & IOS. it might be related to permission. I'm not sure how to set permission for IOS, but for android simulator, you can add following content in AndroidManifest.xml:

<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
jfversluis commented 2 years ago

@VincentBu or others, could you please provide a reproduction project? Because I can't seem to get this issue to show up

ghost commented 2 years ago

Hi @naweed. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

VincentBu commented 2 years ago

could you please provide a reproduction project? Because I can't seem to get this issue to show up

Here is the repro project: MauiApp1.zip Click on 'Get Location' button, you will get error message like this: image

then, add

<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

to AndroidManifest.xml and run again, you would get the expect result: image

naweed commented 2 years ago

This was a very old issue. May be there was a bug in that build. Plz close.