GPS Location Enable in Xamarin forms

DeepakDeepsDeepakDeeps INMember ✭✭✭
edited January 2018 in Xamarin.Forms

In Xamarin forms ,I have to enable GPS like as below image not in settings page

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Ok. Is there a point you're trying to make? Or a question you want to ask? or are you just updating everyone on your project?

  • DeepakDeepsDeepakDeeps INMember ✭✭✭

    I want to know how to request location access from user through app. I tried permissions plugin. I had some issues with it.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    If the user has the GPS on their device turned off, it doesn't matter if you have the permission. You just have to pester them to turn the GPS on, you can't do it programmatically.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Ohhhh... Is that what he was trying to say? That he wanted to override the user's wishes about being tracked?

  • DeepakDeepsDeepakDeeps INMember ✭✭✭

    Currently for enabling GPS switching to Location setting page for enabling GPS....actually i don't wan't to go Location setting page, i want to enable GPS Location as i maintained in below image

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Like I said, you can't do that. The only thing you can do programmatically to get the user to turn on their GPS is send them to the device settings.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @DeepakDeeps said:
    actually i don't wan't to go Location setting page, i want to enable GPS Location as i maintained in below image

    I'll say it less politically correct than Joe:

    It doesn't matter what you want. The manufacturer of the OS and the device are going to tell you want you can do within the accepted expectations for user privacy and security. You don't get to secretly turn on GPS so you can track people without them expressly approving it and turning it on themselves. Period. Get over it.

    If you are making an enterprise grade app for internal corporate use find and pay for an MDM solution such as Samsung Knox that will let you configure the corporate-owned devices before being deployed to the employees.

  • disjesdisjes Member ✭✭

    @JoeManke hi, could you find a solution for both IOS and Android?

  • disjesdisjes Member ✭✭

    @DeepakDeeps did you get this working?

  • DeepakDYDeepakDY INMember ✭✭✭

    @JoeManke said:
    Actually, I stand corrected. It is possible to present a dialog where pressing OK will turn on the GPS. It requires installing the NuGet package Xamarin.Android.GooglePlayServices.Location.

    https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    https://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page

    I Implement this and it working in Xamarin android :smile:

  • MakMak INMember ✭✭
    edited June 11

    @DeepakDY I am trying to implement with SettingsClient in XF. Can you please help me to understand what should be passed in activity parameter to StartResolutionForResult method. Need to pass activity as parameter and activity should override OnActivityResult to get result if user agreed to turn on location or denied as per my understanding. How did you achieved that?
    @JoeManke can you suggest something?

  • DeepakDYDeepakDY INMember ✭✭✭
    edited June 11

    Hello @Mak
    This my working code
    But It requires installing the NuGet package Xamarin.Android.GooglePlayServices.Location.

       public const int REQUEST_CHECK_SETTINGS = 0x1;
      private void DisplayLocationSettingsRequest()
            {
                var googleApiClient = new GoogleApiClient.Builder(this).AddApi(LocationServices.API).Build();
                googleApiClient.Connect();
    
                var locationRequest = LocationRequest.Create();
                locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
                locationRequest.SetInterval(10000);
                locationRequest.SetFastestInterval(10000 / 2);
    
                var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest);
                builder.SetAlwaysShow(true);
    
                var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build());
                result.SetResultCallback((LocationSettingsResult callback) =>
                {
                    switch (callback.Status.StatusCode)
                    {
                        case LocationSettingsStatusCodes.Success:
                            {
                                //DoStuffWithLocation();
                                break;
                            }
                        case LocationSettingsStatusCodes.ResolutionRequired:
                            {
                                try
                                {
                                    // Show the dialog by calling startResolutionForResult(), and check the result
                                    // in onActivityResult().
                                    callback.Status.StartResolutionForResult(this, REQUEST_CHECK_SETTINGS);
                                }
                                catch (IntentSender.SendIntentException e)
                                {
                                }
    
                                break;
                            }
                        default:
                            {
                                // If all else fails, take the user to the android location settings
                                StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings));
                                break;
                            }
                    }
                });
            }
    
     protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
            {
                try
                {
    
                    base.OnActivityResult(requestCode, resultCode, data);
    
                    if (requestCode == 1)
                    {
                        //GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
                        //GoogleManager.Instance.OnAuthCompleted(result);
                    }
                    //Stormlion.ImageCropper.Droid.Platform.OnActivityResult(requestCode, resultCode, data);
                }
                catch (Exception ex)
                {
                }
            }
    

    Let me if this code will work for you or not

  • MakMak INMember ✭✭

    @DeepakDY thank you very much for sharing it. As I said I am trying to implement with SettingsClient in Xamarin Forms. Let me try if it doesn't work this will be really helpful to me.

Sign In or Register to comment.