title: Location permission for Android above 6.0 with Xamarin.Forms.Maps
body: <p>I'm trying to implement a Xamarin.Forms application using Xamarin.Forms.Maps, however I always fall into the exception:</p>
  
  <p>Java.Lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION</p>
  
  <p>I have already tried following the steps in this tutorial <a href="https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/" rel="noreferrer">https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/</a> but did not get results.</p>
  
  <p>Would anyone have something more consistent to help me?</p>
  
  <p>What I'm intending is to use an application very close to Uber and I can not progress if I can not get the location permissions.</p>
  
  <p>My MainPage looks like this in my PCL:</p>
  
  <pre><code>public partial class MainPage: ContentPage
      {
          public MainPage ()
          {
              InitializeComponent ();
              var map = new Map ()
              {
                  MapType = MapType.Street,
                  IsShowingUser = true
              };
  
              var stack = new StackLayout {Spacing = 0};
              stack.Children.Add (map);
  
              Content = stack;
          }
      }
  </code></pre>
  
  <p>My MainActivity class in Xamarin.Android looks like this:</p>
  
  <pre><code>[Activity (Label = "ExampleGeolocation", Icon = "@ drawable / icon",
                Theme = "@ style / MainTheme", MainLauncher = true,
                ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
  
      public class MainActivity: global :: Xamarin.Forms.Platform.Android.FormsAppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
      {
          View layout;
          const int RequestLocationId = 0;
          readonly string [] PermissionsLocation = {Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation};
  
          protected override void OnCreate (Bundle bundle)
          {
              TabLayoutResource = Resource.Layout.Tabbar;
              ToolbarResource = Resource.Layout.Toolbar;
  
              base.OnCreate (bundle);
  
              global :: Xamarin.Forms.Forms.Init (this, bundle);
              global :: Xamarin.FormsMaps.Init (this, bundle);
  
              Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
  
              SetStatusBarColor (Android.Graphics.Color.Black);
              LoadApplication (new App ());
          }
  
          public override async void OnRequestPermissionsResult (int requestCode, string [] permissions, [GeneratedEnum] Permission [] grantResults)
          {
              switch (requestCode)
              {
                  case RequestLocationId:
                      {
                          if (grantResults [0] == Permission.Granted)
                          {
                              var snack = Snackbar.Make (layout, "Location Permission is Available.", Snackbar.LengthShort);
                              snack.Show ();
  
                              await GetLocationAsync ();
                          }
                          else
                          {
                              var snack = Snackbar.Make (layout, "Location Permission Denied", Snackbar.LengthShort);
                              snack.Show ();
                          }
                      }
  
                      break;
              }
          }
  
          async Task TryGetLocationAsync ()
          {
              if ((int) Build.VERSION.SdkInt &lt;23)
              {
                  await GetLocationAsync ();
                  return;
              }
  
              await GetLocationPermissionAsync ();
          }
  
          async Task GetLocationPermissionAsync ()
          {
              const string permission = Manifest.Permission.AccessFineLocation;
  
              if (CheckSelfPermission (permission) == Permission.Granted)
              {
                  await GetLocationAsync ();
                  return;
              }
  
              if (ShouldShowRequestPermissionRationale (permission))
              {
                  Snackbar.Make (layout, "You must allow the application to access your location options.", Snackbar.LengthIndefinite)
                          .SetAction ("OK", v =&gt; RequestPermissions (PermissionsLocation, RequestLocationId))
                          .Show();
  
                  return;
              }
  
              RequestPermissions (PermissionsLocation, RequestLocationId);
          }
  
          async Task GetLocationAsync ()
          {
              try
              {
                  var locator = CrossGeolocator.Current;
              }
              catch (Exception e)
              {
                  e.ToString ();
                  throw new NotImplementedException ();
              }
          }
      };
  </code></pre>