I'm trying to implement a Xamarin.Forms application using Xamarin.Forms.Maps, however I always fall into the exception:
Java.Lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
I have already tried following the steps in this tutorial https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/ but did not get results.
Would anyone have something more consistent to help me?
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.
My MainPage looks like this in my PCL:
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;
????????}
????}
My MainActivity class in Xamarin.Android looks like this:
[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 <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 => RequestPermissions (PermissionsLocation, RequestLocationId))
????????????????????????.Show();
????????????????return;
????????????}
????????????RequestPermissions (PermissionsLocation, RequestLocationId);
????????}
????????async Task GetLocationAsync ()
????????{
????????????try
????????????{
????????????????var locator = CrossGeolocator.Current;
????????????}
????????????catch (Exception e)
????????????{
????????????????e.ToString ();
????????????????throw new NotImplementedException ();
????????????}
????????}
????};
See Question&Answers more detail:os