Problem geolocation in Xamarin

1

I have a problem with the plugin Geolocator it gives me the error of the authorization of the photos:

This is the xaml code :

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Test"
             x:Class="Test.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <Label x:Name="Lat"  Text="Latitud" 
           HorizontalOptions="Center"
           VerticalOptions="Center" />
        <Label x:Name="Long"  Text="Logitud" 
           HorizontalOptions="Center"
           VerticalOptions="Center" />
        <Button x:Name ="TestB"  
          Text="Hola"  
          Clicked="Button_Clicked"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"/>
    </StackLayout>

</ContentPage>

This is the code c # :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Plugin.Geolocator;
namespace Test
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 20;

            var position = await locator.GetPositionAsync();

            Lat.Text = "Latitude: " + position.Latitude.ToString();
            Long.Text = "Longitude: " + position.Longitude.ToString();
        }
    }
}

Manifest

Error

    
asked by Neowolf 22.05.2018 в 19:51
source

1 answer

0

Make sure that the OnCreate method of MainActivity.cs performs the registration of the CrossCurrentActivity Plugin, as follows:

protected override void OnCreate(Bundle bundle)
{            
    // Código de tu clase
    // ............

    CrossCurrentActivity.Current.Activity.Init(this, bundle);

    // Código de tu clase
    // ............
}

Then add the following code to the AssemblyInfo.cs file of your Android project:

[assembly: UsesFeature("android.hardware.location", Required = false)]
[assembly: UsesFeature("android.hardware.location.gps", Required = false)]
[assembly: UsesFeature("android.hardware.location.network", Required = false)]

Additionally this plugin needs to set permissions by overwriting the following method in your MainActivity.cs within the Android project:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
    Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Taken from github's help: link

    
answered by 22.05.2018 / 21:10
source