我正在开发一个基于GPS服务的应用程序,我必须连续跟踪用户的位置,例如HERE Maps。
geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.MovementThreshold = 20; //Doesn't matter the value I put here, it won't work
geolocator.PositionChanged += geolocator_PositionChanged;
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
if(args.Position != null)
{
myPosition = args.Position.Coordinate.ToGeoCoordinate();
UpDateData();
}
});
}
(我曾尝试使用GeoCoordinateWatcher,但我什么也没得到)。 当我站在同一个地方或移动得非常慢时,这些功能可以很好地工作,但是如果我进入汽车并在几秒钟后开始驱动应用程序崩溃,我不知道为什么。 我搜索了大量具有相同终结性的代码,但所有代码都不起作用。 你知道这个问题的其他任何解决方案,还是已经发现你和我的问题一样?
答案 0 :(得分:0)
try
{
geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.ReportInterval = 2000;
geolocator.PositionChanged += geolocator_PositionChanged;
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Location is Disabled in Phone Settings.");
}
private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
try
{
Dispatcher.BeginInvoke(() =>
{
if (args.Position != null && args.Position.Coordinate.ToGeoCoordinate() != myPosition)
{
if(args.Position.Coordinate.Accuracy <= 1500)
{
myPosition = args.Position.Coordinate.ToGeoCoordinate();
UpDateMyPositionCircle(args.Position.Coordinate.Accuracy);
}
}
});
}
catch (TargetInvocationException tie)
{
if (tie.Data == null) throw;
else MessageBox.Show("TargetInvocationException while Tracking: " + tie.InnerException.ToString());
}
catch(SystemException se)
{
if (se.Data == null) throw;
else MessageBox.Show("SystemException while Tracking: " + se.InnerException.ToString());
}
catch(Exception ex)
{
if (ex.Data == null) throw;
else MessageBox.Show("Exception while Tracking: " + ex.InnerException.ToString());
}
}