Enable Location Service in .NET MAUI after upgrading from Xamarin Forms

Etien Ducka 30 Reputation points
2024-03-08T11:39:27.1466667+00:00

I am currently upgrading my Xamarin Forms application to .NET MAUI. In my Xamarin Forms app, I used DependencyService to enable a location pop-up asking the user for location service permission. I'm struggling to find a way to achieve the same result in .NET MAUI. Can anyone help me out? Note that this is for production.

IMG_20240213_173615

Here is the code I used in my Xamarin Forms app:

//MainPage.cs
DependencyService.Get<ILocationSettings>().EnableLocation();

//Separate .cs at main Program
public interface ILocationSettings 
{
     void EnableLocation();
}

//And at MainProgram.Android I have 
[assembly: Dependency(typeof(LocationSettings))]
namespace MainProgram.Droid  
{
    class LocationSettings : ILocationSettings
    {  
        public async void EnableLocation()
        {
            try
            {
                Android.Gms.Location.LocationRequest locationRequest = Android.Gms.Location.LocationRequest.Create();
                locationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityHighAccuracy);
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest);
                var response = await LocationServices.GetSettingsClient(Platform.CurrentActivity).CheckLocationSettingsAsync(builder.Build());
            }
            catch (ApiException exception)
            {
                switch (exception.StatusCode)
                {
                    case LocationSettingsStatusCodes.ResolutionRequired:
                        ResolvableApiException resolvable = (ResolvableApiException)exception;
                        resolvable.StartResolutionForResult(Platform.CurrentActivity, 0x1);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,026 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,011 Reputation points Microsoft External Staff
    2024-03-11T07:26:17.23+00:00

    Hello,

    Before you start to use Api to access the Google location services, please install following nuget packages in your application.

    Xamarin.AndroidX.Fragment.Ktx
    
    Xamarin.GooglePlayServices.Location
    

    Then, Android.Gms.Location.LocationRequest.Create is deprecated. Please use Android.Gms.Location.LocationRequest.Builder to do it.

    For MAUI, you do not need to use Dependence service to invoke specific platform code. Please use Conditional compilation

    You can refer to the following code.

    private async void OnCounterClicked(object sender, EventArgs e)
            {
    #if ANDROID
                try
                {
                   
                    var locationRequest =new Android.Gms.Location.LocationRequest.Builder(Priority.PriorityHighAccuracy,100);
                    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest.Build());
                    var response = await LocationServices.GetSettingsClient(Platform.CurrentActivity).CheckLocationSettingsAsync(builder.Build());
                }
                catch (ApiException exception)
                {
                    switch (exception.StatusCode)
                    {
                        case LocationSettingsStatusCodes.ResolutionRequired:
                            ResolvableApiException resolvable = (ResolvableApiException)exception;
                            resolvable.StartResolutionForResult(Platform.CurrentActivity, 0x1);
                            break;
                        default:
                            break;
                    }
                }
    
    
    #endif
            }
    

    As note: please do not forget to add used Namespace in Conditional compilation.

    #if ANDROID
    using Android.Gms.Common.Apis;
    using Android.Gms.Location;
    #endif
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.