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.