xamarin.forms - 每 1 分钟定位一次
问题描述
我正在创建一个 xamarin 表单应用程序,无论应用程序是在前台还是后台运行,每 1 分钟后需要用户位置(用户的经度和纬度)。如果用户停留在一个位置或缓慢移动,我能够在每 1 分钟后获取用户位置。
但是当用户在骑自行车或高速移动的情况下面临获取位置的问题,我无法在这种情况下获取用户的位置(用户经度和纬度)。
try
{
if (CrossGeolocator.Current.IsGeolocationAvailable)
{
if (CrossGeolocator.Current.IsGeolocationEnabled)
{
count += 1;
// var location = await GetLocationFromPhone().ConfigureAwait(false);
var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10)));
if (location != null)
{
var model = new lat_lon
{
lat = location.Latitude,
lon = location.Longitude,
dist = count
};
MessagingCenter.Send<lat_lon>(model, "LocationLngLet");
}
}
}
}
catch (TargetInvocationException srtiex)
{
}
catch (IllegalStateException ilsex)
{
}
catch (FeatureNotEnabledException fneex)
{
}
catch (NullReferenceException nrex)
{
}
catch (FeatureNotSupportedException fnsEx)
{
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (System.Exception ex)
{
// Unable to get location
}
建议任何解决这个问题的方法!!
解决方案
这应该在服务中运行(https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/,https://docs.microsoft.com/en-us/xamarin/ios/ app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/location-walkthrough),因为无论手机/应用程序的状态如何,您都需要它工作。考虑为每个项目使用 LocationManagers 并设置 locationChanged 所需的属性。
推荐阅读
- python - 如何将excel密码文件导入python?
- javascript - Javascript:尝试在开关中使用 var:“已声明 var 但从未读取其值”
- c# - 字符串类型的字典中缺少给定的键
- javascript - javascript模块中未定义父类
- php - 在下拉菜单中编辑显示的用户组
- karate - SNI:SSLHandshakeException unrecognized_name
- node.js - 按天聚合产生重复的天结果
- azure - 如何将 azure api 管理集成到节点 socket.io 应用程序
- list - 删除重复的小部件
- java - 如何修复接收对象数组的静态泛型方法