我正在尝试创建一个应用程序。在这里,我试图通过单击按钮获取用户的当前位置。但它会产生一个异常,例如Not Implemented异常 - 此功能在此程序集的可移植版本中未实现。您应该从主应用程序项目中引用NuGet包,以引用特定于平台的实现
string answer ="";
try
{
await CrossGeolocator.Current.StartListeningAsync(new
TimeSpan(20000),10);
if (CrossGeolocator.Current.IsListening)
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await
locator.GetPositionAsync(TimeSpan.FromSeconds(20000));
string lat = position.Latitude.ToString();
string lon = position.Longitude.ToString();
answer = lat + lon;
}
else
{
answer= "Not listening";
}
}
catch (Exception ex)
{
answer= ex.Message;
}
我需要包含经度和纬度值的结果。我在项目中要做什么?
编辑:
public async Task<String> GetLastKnownLocationAsync()
{
Position ObjPosition = null;
try
{
await DependencyService.Get<IGeolocator>().StartListeningAsync(new TimeSpan(20000), 10);
if (CrossGeolocator.Current.IsListening)
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
ObjPosition = await DependencyService.Get<IGeolocator>().GetPositionAsync(TimeSpan.FromSeconds(20));
string lat = ObjPosition.Latitude.ToString();
string lon = ObjPosition.Longitude.ToString();
Info = lat + lon;
}
else
{
Info = "Not listening";
}
return Info;
}
catch (Exception ex)
{
Info = ex.Message;
return Info;
}
}
我在第六行遇到错误。
我也在我的Xamarin.Forms应用程序中实现了GPS跟踪功能。根据我的个人经验,Geolocator插件不能像Xamarin.Forms那样正常工作,也有一些问题和限制。这个插件是参考Xamarin Essentials Geolocation开发的。我建议你应该使用Xamarin Essentials而不是Geolocator插件,因为它有很好的文档记录,易于实现,没有任何重大问题。您可以通过以下链接找到分步指导来实施Xamarin Essentials Geolocation:
https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android
您是否在共享项目以及平台项目上安装了NuGet包?在这种情况下,错误消息非常准确。这里基本上发生的是这个插件安装了一种依赖服务形式,它有一个特定于平台的实现,只是你共享项目上的一个接口。
出于某种原因,您的调用最终会出现在共享代码中,该代码只会实现此异常,以告知您位于错误的位置。这通常是由于没有在您的平台项目上安装软件包,或者软件包被链接器“优化掉”。这往往会发生,因为编译器注意到您的项目没有引用到此库,因此它将其删除以使其占用更少的空间。
为了确保最后这件事没有发生,您可以进入您的平台项目,在这种情况下,Android并进入MainActivity.cs
并在此插件中添加对象的虚拟引用。例如,添加以下内容:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
// THIS WAS ADDED
var foo = new Plugin.Geolocator.Abstractions.Address();
LoadApplication(new App());
}
这实际上是很多这些库过去使用Init
方法的原因。
说完这一切之后,我实际上必须在另一个答案中同意Akshay,如果可能的话,你可能想看看升级你的项目以使用.NET Standard并转移到Xamarin.Essentials库,这会带来所有这些痛苦远。
这里有一些建议,我通常会使用:如果您遇到问题并且无法解决问题,请尝试创建一个单独的小项目,其中包含有关其工作原理的分步教程。通常它可以工作,你可以找出主要解决方案的确切错误。
编辑:
不久,您必须创建将由您使用的接口,即IMyInterface
。之后,使用接口实现为Android / iOS编写特定于平台的类。当你编写它时,你可以使用如下方法:DependencyService.Get<IMyInterface>().YourMethod()
。
编辑2:
您必须为特定于平台的类添加此项:
[assembly: Dependency(typeof(MyCustomClass))] // Check this assembly
namespace ISSO_I.Droid.PlatformSpecific
{
public class MyCustomClass: IMyInterface
{
/// your code here
}
}
编辑3:
呼叫位置更新:
protected override void OnAppearing()
{
base.OnAppearing();
ToIssoView = false;
RequestLocationUpdates(); // Call this method
}
请求位置更新的方法:
public async void RequestLocationUpdates()
{
/// check permission for location updates
var hasPermission = await CommonStaffUtils.CheckPermissions(Permission.Location);
if (!hasPermission)
return;
if (CrossGeolocator.Current.IsListening) return;
MyMap.MyLocationEnabled = true;
CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
CrossGeolocator.Current.PositionError += Current_PositionError;
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 5);
}
public static async Task<bool> CheckPermissions(Permission permission)
{
var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
var request = false;
if (permissionStatus == PermissionStatus.Denied)
{
if (Device.RuntimePlatform == Device.iOS)
{
var title = $"{permission} Permission";
var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
const string positive = "Settings";
const string negative = "Maybe Later";
var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
if (task == null)
return false;
var result = await task;
if (result)
{
CrossPermissions.Current.OpenAppSettings();
}
return false;
}
request = true;
}
if (!request && permissionStatus == PermissionStatus.Granted) return true;
{
var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
if (!newStatus.ContainsKey(permission) || newStatus[permission] == PermissionStatus.Granted) return true;
var title = $"{permission} Permission";
var question = $"To use the plugin the {permission} permission is required.";
const string positive = "Settings";
const string negative = "Maybe Later";
var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
if (task == null)
return false;
var result = await task;
if (result)
{
CrossPermissions.Current.OpenAppSettings();
}
return false;
}
}