Windows手机应用程序中的位置跟踪问题

本文关键字:位置 跟踪 问题 手机 应用程序 Windows | 更新日期: 2023-09-27 17:54:20

我的应用程序的一些用户抱怨,跟踪方面不工作。我有一个GPS图标在屏幕的顶部闪烁,而应用程序启动GeoCoordinateWatcher,并试图获得当前位置。当这是完成,并确定图标停止闪烁和一个消息说准备出现。用户报告说,这种情况发生了,但屏幕上的项目,如速度永远不会更新。当应用程序保存它跟踪的位置时,那里什么也没有。

这是跟踪部分的代码。在页面加载事件中,它调用以下

    /// <summary>
    /// Starts tracking the user
    /// </summary>
    private void StartTracking()
    {
        var app = (Application.Current as App);
        // check to see if tracking is enabled by the user                    
        if (app.LocationTrackingIsEnabled)
        {               
                (new Thread(() =>
                {
                    // Create the GeoWatcher
                    var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High) { MovementThreshold = 1 };
                    // Check to see if we have permission to use the location services of the phone
                    if (watcher.Permission == GeoPositionPermission.Granted)
                    {
                        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
                        var status = Observable.FromEvent<GeoPositionStatusChangedEventArgs>(watcher, "StatusChanged");
                        var readys = status.Where(o => o.EventArgs.Status == GeoPositionStatus.Ready);
                        var notReadys = status.Where(o => o.EventArgs.Status != GeoPositionStatus.Ready);
                        var readyPos = from r in readys
                                       from i in Observable.Interval(TimeSpan.FromSeconds(LocationTrackInterval))
                                       .TakeUntil(notReadys)
                                       where (DateTime.Now - watcher.Position.Timestamp.DateTime) < TimeSpan.FromSeconds(12)
                                       select watcher.Position;
                        LocationSubscribe = readyPos.Subscribe(loc =>
                        {
                            if (!HasPaused)
                            {
                                this.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    // Get current speed (meters per second);
                                    if (!double.IsNaN(loc.Location.Speed))
                                        app.CurrentPos.CurrentSpeed = Math.Round(loc.Location.Speed, 2);
                                    else
                                        app.CurrentPos.CurrentSpeed = 0;
                                    // Calculate distance
                                    if (RunLocations.Count > 0)
                                        app.CurrentPos.DistanceMeters += Math.Round(new GeoCoordinate(RunLocations[RunLocations.Count - 1].Latitude,
                                            GPSLocations[GPSLocations.Count - 1].Longitude).GetDistanceTo(loc.Location), 2);
                                    // Add Location
                                    GPSLocations.Add(new GPSLocation()
                                    {
                                        Latitude = loc.Location.Latitude,
                                        Longitude = loc.Location.Longitude,
                                        Altitude = loc.Location.Altitude,
                                        Speed = app.CurrentRun.CurrentSpeed
                                    });
                                    // Get the average speed
                                    app.CurrentPos.AverageSpeed = Math.Round((from r in GPSLocations
                                                                              select r.Speed).Average(), 2);

                                    // Set last position for use later
                                    Lastlocation = loc.Location;
                                }));
                            }
                        });
                        // Try and start the watcher
                        if (!watcher.TryStart(false, TimeSpan.FromSeconds(5)))
                        {
                            this.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                MessageBox.Show("There was an error trying to get your location. Tracking is not possible.");
                            }));
                        }
                    }
                    else
                    {
                        sbGpsFlash.Stop(); // stop the flashing gps symbol
                        gpsStatus.Text = "Denied";
                    }
                })).Start();                
        }
        else
        {
            sbGpsFlash.Stop(); // stop the flashing gps symbol
            gpsStatus.Text = "Disabled";
        }
    }
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
         this.Dispatcher.BeginInvoke(new Action(() =>
         {
             gpsStatus.Text = e.Status.ToString();
             switch (e.Status)
             {
                 case GeoPositionStatus.Initializing:
                     gpsStatus.Text = "Locating...";
                     break;
                 case GeoPositionStatus.Disabled:
                     gpsStatus.Text = "Disabled";
                     break;
                 case GeoPositionStatus.NoData:
                     gpsStatus.Text = "No Data";
                     break;
                 case GeoPositionStatus.Ready:
                     gpsStatus.Text = "Ready";
                     break;                                     
             }
             sbGpsFlash.Stop();
         }));
    }

谁能看到可能导致问题的代码问题?

Windows手机应用程序中的位置跟踪问题

在主线程上创建geocoordinatewatcher

附注。

你的代码很紧凑,但不是很好读

您不必在主线程上创建坐标监视器。

我想知道你只订阅状态事件而不订阅位置更改事件,但这是你的逻辑,如果它对你来说很好,那就很好。

点是你想要在ui上显示的数据只能分配给创建ui控件的线程中的ui控件,通常这是一个人们称为"主"线程的线程。我不知道为什么这是主线。事实上,我将其称为UI线程。

所以你必须在UI线程中传递你的数据你的UI控件是在其中创建的。因此,可以在UI线程上启动监视器,或者使用UI线程的Dispatcher将数据传递给它,并将数据分配给UI控件。