Xamarin Forms App Enables GPS location

ΝΤΟΥΤΣΚΑ ΕΤΙΕΝ 96 Reputation points
2021-04-08T11:52:38.82+00:00

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 .

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. ΝΤΟΥΤΣΚΑ ΕΤΙΕΝ 96 Reputation points
    2021-04-19T11:59:23.867+00:00

    [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 !


2 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-04-09T08:30:29.597+00:00

    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.


  2. ΝΤΟΥΤΣΚΑ ΕΤΙΕΝ 96 Reputation points
    2021-04-19T11:45:13.017+00:00

    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

    0 comments No comments

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.