地理定位器位置改变事件 [英] Geolocator position changed event

查看:30
本文介绍了地理定位器位置改变事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个正在运行的跟踪器/计步器应用程序,我正在使用地理定位器,我将地理定位器的移动阈值属性保持为 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(地理定位器发送者,PositionChangedEventArgs args){Dispatcher.BeginInvoke(() =>{var coord = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);如果(_line.Path.Count > 0){var previousPoint = _line.Path.Last();距离 += coord.GetDistanceTo(previousPoint);var millisPerKilometer = (1000.0/distance) * (System.Environment.TickCount - _previousPositionChangeTick);_km += Math.Round(distance, 2);distanceLabel.Text = string.Format("{0:f2} 米", _kilometres);MessageBox.Show("已更改");}别的{Map.Center = 坐标;}_line.Path.Add(coord);_previousPositionChangeTick = System.Environment.TickCount;});}

问题是位置更改事件只被调用一次,我试图通过更改位置点来调试模拟器中的代码,但仍然没有调用该事件.我哪里做错了??

解决方案

您的代码将在真实设备上运行.但是,为了在模拟器上进行测试,请尝试将 DesiredAccuracy 属性设置为 High.

来自 如何测试使用 Windows Phone 位置数据的应用:

<块引用>

如果您的应用程序使用 GeoCoordinateWatcher 类,您必须在构造函数或类的 DesiredAccuracy 属性中指定 GeoPositionAccuracy.High 的值,然后才能使用位置传感器模拟器测试您的应用程序.如果您将精度保留为 GeoPositionAccuracy.Default 的默认值,则 PositionChanged 事件无法识别位置传感器模拟器中发生的位置变化.

还有另一种解决方法,即运行本机地图应用程序,这似乎可以解决问题:

<块引用>

  1. 在模拟器中设置当前位置.
  2. 运行您的应用.它将当前位置报告为 Redmond.
  3. 运行地图应用程序.它正确地转到设置的位置第 1 步.
  4. 再次运行您的应用.现在它使用正确的当前位置.

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

I am developing a running tracker/pedometer app, I am using geolocator for the same,I am keeping the movement threshold property of the geoLocator to 10 here is my piece of code.

button click event

 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;
            });
        }

The problem is that the position changed event is only getting called once, I am trying to debug the code in emulator by changing the location points but still the event do not get called. where am I doing wrong??

解决方案

Your code will work on a real device. However, in order to test on the emulator, try by setting the DesiredAccuracy property to High.

From 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.

There is also another workaround, that consists on running the native Maps app, which seems to fix the problem:

  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.

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

这篇关于地理定位器位置改变事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆