How to Get Latitude and longitude using Xamarin.Essentials?

时间:2019-04-08 12:57:05

标签: xamarin.forms

I'm trying to get lat and long using the Xamarin.Essentials package but I'm getting an exception

exception: this functionality is not implemented in the portable version of this assembly. you should reference the nuget package from your main application project in order to reference the platform-specific implementation. at xamarin.essentials.geolocation.platformlocationasync" i installed the package to every platform

Here is my code:

private async void Button_Clicked(object sender, EventArgs e)
{
    try
    {
        var request = new GeolocationRequest(GeolocationAccuracy.Medium);
        var location = await Geolocation.GetLocationAsync(request);

        if (location != null)
        {
            latitude.Text = location.Latitude.ToString();
            longitude.Text = location.Longitude.ToString();
        }
    }
    catch (FeatureNotEnabledException fnex)
    {

    }
    catch (PermissionException perex)
    {

    }
    catch (Exception ex)
    {
        await DisplayAlert("Error3", ex.ToString(), "ok");
    }
}

2 个答案:

答案 0 :(得分:0)

根据此问题报告:https://github.com/xamarin/Essentials/issues/379

您需要将“编译目标版本”设置为Android 8.1

答案 1 :(得分:0)

从2019年3月开始,Play商店将不接受任何针对Android版本低于API-26(8.1)的应用程序(与上述例外相同的原因),所以我建议您对其进行更新。

此外,您的设备范围符合条件,所以这不是问题(请参阅下文),

  

Xamarin.Essentials专注于以下平台:

     
      
  • iOS(10 +)
  •   
  • Android(4.4 +)
  •   
  • UWP(秋季创作者更新+)
  •