question

61613576 avatar image
0 Votes"
61613576 asked LeonLu-MSFT commented

Xamarin Forms App Enables GPS location

Hi guys i am new to Xamarin and i cant enable gps location like google maps when it ask you your location.

How i can do that ?

I have enabled permissions and i have a code that gives me location when i have enabled my gps but i dont know what to do when my gps is off to enabled it from the application like google maps .

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Did you achieve runtime permission in your application like this thread? https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android

0 Votes 0 ·

Yeah i did it

Xamarin.Essentials.FeatureNotEnabledException: 'Location services are not enabled on device.'

i get that message.

Gps works when i enable gps Location but i want app to detect if android has gps location enabled or not . Any idea or any other code that i can check ?

0 Votes 0 ·
61613576 avatar image
0 Votes"
61613576 answered LeonLu-MSFT commented

[Obsolete]
public async void EnableItGod()
{

         {
             try
             {
                 MainActivity activity = Forms.Context as MainActivity;

                 GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity)
                     .AddApi(LocationServices.API).Build();
                 googleApiClient.Connect();
                 LocationRequest locationRequest = LocationRequest.Create();
                 locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
                 locationRequest.SetInterval(10000);
                 locationRequest.SetFastestInterval(10000 / 2);

                 LocationSettingsRequest.Builder
                         locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
                         .AddLocationRequest(locationRequest);
                 locationSettingsRequestBuilder.SetAlwaysShow(false);
                 LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync
                     (googleApiClient, locationSettingsRequestBuilder.Build());

                 if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired)
                 {
                     locationSettingsResult.Status.StartResolutionForResult(activity, 0);
                 }
             }
             catch (Exception ex)
             {
              //   GlobalVariables.SendExceptionReport(ex);
             }
         }
     }



I Found it !

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your sharing.

1 Vote 1 ·
LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

Gps works when i enable gps Location but i want app to detect if android has gps location enabled or not . Any idea or any other code that i can check ?

You can create a dependenceService to get the GPS status if is enabled. Here is code.


Create an interface in PCL.

namespace App83
{
    public interface IDetectGPS
    {
        bool isGPSEnabled();
    }
}


Then achieve it in android project.

using Android.App;
using Android.Content;
using Android.Locations;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App83.Droid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;

[assembly: Dependency(typeof(DetectGPSService))]
namespace App83.Droid
{
    public class DetectGPSService : IDetectGPS
    {
        public bool isGPSEnabled()
        {
          
           
            LocationManager locationManager = null;
            bool gps_enabled = false;
           
            if (locationManager == null)
            {
              
                locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
            }
            try
            {
                gps_enabled = locationManager.IsProviderEnabled(LocationManager.GpsProvider);
            }
            catch (Exception ex) { }

            return gps_enabled;
        }
    }
}




Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

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.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.



Thanks for response @LeonLu-MSFT .

Is the same code for Android and Ios ?

0 Votes 0 ·

Its only opening the location window that you can manually enable or disable gps location . it doesnt auto open it.
Any ideas ?

0 Votes 0 ·
Show more comments
61613576 avatar image
0 Votes"
61613576 answered

public async void turnOnGps()
{
try
{
MainActivity activity = Forms.Context as MainActivity;

             GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity)
                 .AddApi(LocationServices.API).Build();
             googleApiClient.Connect();
             LocationRequest locationRequest = LocationRequest.Create();
             locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
             locationRequest.SetInterval(10000);
             locationRequest.SetFastestInterval(10000 / 2);

             LocationSettingsRequest.Builder
                     locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
                     .AddLocationRequest(locationRequest);
             locationSettingsRequestBuilder.SetAlwaysShow(false);
             LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(
                 googleApiClient, locationSettingsRequestBuilder.Build());

             if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired)
             {
                 locationSettingsResult.Status.StartResolutionForResult(activity, 0);
             }
         }
         catch (Exception ex)
         {
             GlobalVariables.SendExceptionReport(ex);
         }
     }




i found it

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.