【问题标题】:Windows Phone: PositionChanged not firing Windows 8Windows Phone:PositionChanged 未触发 Windows 8
【发布时间】:2014-07-18 21:25:48
【问题描述】:

我在使用 windows phone 时遇到问题,我正在查看地理定位器位置更改事件,我实例化并设置 GEOLOCATOR,如下所示:

 geolocator = new Geolocator();
 geolocator.DesiredAccuracy = PositionAccuracy.High;
 geolocator.MovementThreshold = 100; // The units are meters.
 geolocator.PositionChanged += geolocator_PositionChanged;

我的 positionchanged 事件如下:

void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    {
        Dispatcher.BeginInvoke(() =>
        {
            marker = (UserLocationMarker)this.FindName("UserLocationMarker");
            if (marker != null)
            {
                marker.GeoCoordinate = args.Position.Coordinate.ToGeoCoordinate();

            }

        });
    }

当我在模拟器中启动它时,PositionChanged 正确地被启动了 1 次,最后一个位置被输入到 TOOLS 函数中的位置中......但是我添加到定位工具中的任何后续 GEOPOINTS 都不会启动 positionchanged 方法....我不认为它是模拟器本身,就好像我在模拟器上启动地图并使用定位工具一样,它会在每次更新时正确更改用户位置。但是在我的代码中,我对 positionchanged 方法进行了调试,并且仅在应用程序启动时第一次调用它,仅此而已……一旦获得了地理位置,这就是她所写的一切……永远不会触发任何动作在定位工具中。

我的代码中没有任何内容会破坏地理定位器对象...但没有其他输入会触发它...我不知所措..任何对我遗漏或不理解的内容的帮助将不胜感激。在这一点上,我想知道工具包中的 UserLocationMarker 是否以某种方式破坏或删除了位置更改事件侦听器注册或其他东西。

【问题讨论】:

    标签: windows-phone-8 location windows-phone-8.1


    【解决方案1】:

    请参阅此参考以正确响应位置更改事件:MSDN PositionChanged

    确保在 UI 线程上声明 Geolocator。我在我的 Windows Phone 8 上测试了以下代码。我在办公室里走来走去,观察按钮的内容根据我在建筑物中的位置而变化。

    可能是您的 MovementThreshold 太高或模拟器出现问题。

    public partial class MainPage : PhoneApplicationPage
    {    
        Geolocator geo = null;
        public MainPage()
        {
            InitializeComponent();
            geo = new Geolocator();
            try
            {
                geo.MovementThreshold = 1.0;
                geo.DesiredAccuracy = PositionAccuracy.High;
                geo.ReportInterval = 2000;
                geo.PositionChanged += geo_PositionChanged;
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
        }
    
        private void geo_PositionChanged(Geolocator sender, PositionChangedEventArgs e)
        {
            // my_button is just a normal button on the main xaml page
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                this.my_button.Content = e.Position.Coordinate.Latitude.ToString() + "\n" + e.Position.Coordinate.Longitude.ToString();
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      首先查看How to test apps that use location data for Windows Phone 8,了解如何测试使用定位服务的应用的更多知识。

      为了您的信息,这个问题已经得到了答案here。希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        这段代码对我来说很好用,很好用,类似于 HERE 地图应用:

        geolocator = new Geolocator();
        geolocator.DesiredAccuracy = PositionAccuracy.High;
        geolocator.ReportInterval = 2000;
        geolocator.PositionChanged += geolocator_PositionChanged;
        

        无论您距离上次捕获的位置还有多少米,2 秒后,应用程序都会再次捕获您当前的位置 尝试在真机上测试应用

        【讨论】:

          猜你喜欢
          • 2012-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多