Xamarin Geolocator does not return anything to me

2

Hi, I'm using Xamarin and GeoLocator to make an application with Geolocation, but it does not return the data I'm passing, and in debug, it sends me the following error:

This error only appears when I click the button that executes the method I want. Prior to this I throw no error. The first time I ran it, I activated the gps, but when I continued using it, I did not even ...

This is my code:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace OwService {
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class PageGps: ContentPage {
    public PageGps() {
      InitializeComponent();
      btnGetLoc.Clicked += BtnGetLoc_Clickied;

    }

    private async void BtnGetLoc_Clickied(object sender, EventArgs e) {
      await RetriveLocation();
    }

    private async Task RetriveLocation() {
      var locator = CrossGeolocator.Current;
      locator.DesiredAccuracy = 10;

      var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10000), null, true);

      txtLat.Text = "Latitude: " + position.Latitude.ToString();
      txtLong.Text = "Longitude: " + position.Longitude.ToString();
    }

  }
}
    
asked by E.Rawrdríguez.Ophanim 07.02.2018 в 02:07
source

1 answer

3

First, you need to make sure that you have implemented the PermissionsPlugin requirement.

  • In your MainActivity, you need to add the following method.

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    
  • For the Permissions Plugin to work, you need to make sure that in your OnCreate method you set the CurrentActivity. That means adding the following line before LoadApplication(new App());

    Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
    
  • Finally, you need to make sure you take into account the permissions. I would recommend changing your implementation as follows.

            public async Task RetriveLocation()
            {
                try
                {
                    var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
                    if (status != PermissionStatus.Granted)
                    {
                        if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
                        {
                            await DisplayAlert("Need location", "Gunna need that location", "OK");
                        }
    
                        var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
                        //Best practice to always check that the key exists
                        if (results.ContainsKey(Permission.Location))
                            status = results[Permission.Location];
                    }
    
                    if (status == PermissionStatus.Granted)
                    {
                        var position = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromMilliseconds(10000));
                        txtLat.Text = "Latitude: " + position.Latitude;
                        txtLong.Text = "Longitude: " + position.Longitude;
                    }
                    else if (status != PermissionStatus.Unknown)
                    {
                        await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
                    }
                }
                catch (Exception ex)
                {
    
                    txtLat.Text = "Error: " + ex;
                    txtLong.Text = "Error: " + ex;
                }
            }
    
      

    Here is a test repository that works with the code mentioned above. link

      

    Source: link

        
    answered by 15.02.2018 / 20:32
    source