我正在开发一个运行跟踪器/计步器应用程序,我正在使用地理定位器,我将地理定位器的运动阈值属性保持为 10 这是我的代码。
按钮点击事件
private void StartButton_Click(object sender, RoutedEventArgs e)
{
myLocator = new Geolocator();
myLocator.DesiredAccuracy = PositionAccuracy.Default;
myLocator.MovementThreshold = 10;
myLocator.ReportInterval=500;
myLocator.PositionChanged += myGeoLocator_PositionChanged;
_startTime = System.Environment.TickCount;
_timer.Start();
}
void myGeoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
var coord = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
if (_line.Path.Count > 0)
{
var previousPoint = _line.Path.Last();
distance += coord.GetDistanceTo(previousPoint);
var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
_kilometres += Math.Round(distance, 2);
distanceLabel.Text = string.Format("{0:f2} meters", _kilometres);
MessageBox.Show("Changed");
}
else
{
Map.Center = coord;
}
_line.Path.Add(coord);
_previousPositionChangeTick = System.Environment.TickCount;
});
}
问题是位置更改事件仅被调用一次,我试图通过更改位置点来调试模拟器中的代码,但事件仍然没有被调用。我哪里做错了??
最佳答案
您的代码将在真实设备上运行。但是,为了在模拟器上进行测试,请尝试将 DesiredAccuracy
属性设置为 High
。
来自How to test apps that use location data for Windows Phone :
If your app uses the GeoCoordinateWatcher class, you have to specify a value of GeoPositionAccuracy.High in the constructor or in the DesiredAccuracy property of the class before you can test your app with the location sensor simulator. If you leave the accuracy at its default value of GeoPositionAccuracy.Default, the PositionChanged event doesn’t recognize position changes that occur in the location sensor simulator.
还有另一种解决方法,即运行 native map 应用程序,这似乎可以解决问题:
- Set a current location in the emulator.
- Run your app. It reports the current location as Redmond.
- Run the Maps application. It correctly goes to the location set in step 1.
- Run your app again. Now it uses the correct current location.
关于windows-phone-8 - 地理定位器位置更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18382134/