我有一个任务,按位置找到最近的博物馆,但它只在手机上工作。我不知道我是否可以让它工作的计算机以及?
[ICommand]
async Task GetClosestMuseum(MuseumModels museum)
{
//if (IsBusy || Museums.Count == 0)
// return;
try
{
// Get cached location, else get real location.
var location = await geolocation.GetLastKnownLocationAsync();
if (location is null)
{
location = await geolocation.GetLocationAsync(new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.Medium,
Timeout = TimeSpan.FromSeconds(30)
});
}
if (location is null) return;
// Find closest monkey to us
var first = Museums.OrderBy(m => location.CalculateDistance(
Convert.ToDouble(m.Latitude), Convert.ToDouble(m.Longitude), DistanceUnits.Miles)).FirstOrDefault();
if (first is null) return;
await Shell.Current.DisplayAlert("Closest Museum", $"{first.Name} in {first.Address}", "OK");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
await Shell.Current.DisplayAlert("Error!", $"Unable to get closest museum:{ex.Message}", "OK");
}
}
1条答案
按热度按时间xwbd5t1u1#
我测试了你提供的代码,也满足Windows平台无法访问位置的问题。
但我解决了这个问题。在Windows 11中,在位置隐私设置中,我的位置服务未打开。所以,我打开了它,现在它工作了。
此外,如果你也想在Android平台上运行它,记得把这个添加到你的AndroidManifest中: