windows-phone-8 - 地理定位器位置更改事件

标签 windows-phone-8 windows-phone-8-emulator

我正在开发一个运行跟踪器/计步器应用程序,我正在使用地理定位器,我将地理定位器的运动阈值属性保持为 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 应用程序,这似乎可以解决问题:

  1. Set a current location in the emulator.
  2. Run your app. It reports the current location as Redmond.
  3. Run the Maps application. It correctly goes to the location set in step 1.
  4. Run your app again. Now it uses the correct current location.

来源:http://social.msdn.microsoft.com/Forums/wpapps/en-US/c2cc57b1-ba1f-48fb-b285-d6cfbb8f393a/windows-phone-8-emulator-returns-microsofts-location-only

关于windows-phone-8 - 地理定位器位置更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18382134/

相关文章:

javascript - 如何从包含 onclick 事件的文本中删除突出显示?

c# - 如何在 "Pressed"VisualState 的 ContentControl 中自定义属性?

jquery-mobile - jQueryMobile-Phonegap : Footer alignment issue in Windows8 Landscape mode

windows-phone-8 - 将 Windows Phone 8 模拟器(在 VirtualBox 上)连接到 Visual Studio(在主机上)

visual-studio - Visual Studio 2013 Windows Phone Emulator 未部署

c# - 如何将文件写入 Internet Explorer Mobile 的响应?

c# - 适用于 Windows Phone 8 的多语言应用程序工具包,使用代码 C#

xaml - 为什么在 WXGA 模拟器中 SystemTray 有一个底部边框?

windows-phone-8.1 - 意外错误 : -2147483638 while deploying windows8. 1 应用程序给我的设备

windows-phone-8 - Windows Phone 8.1模拟器未加载操作系统