Windows Phone应用中的位置跟踪问题

时间:2013-01-18 07:13:18

标签: c# windows-phone-7

我的应用的一些用户抱怨跟踪方不起作用。当应用程序启动GeoCoordinateWatcher并尝试获取当前位置时,我在屏幕顶部有一个GPS图标闪烁。完成此操作后,图标将停止闪烁,并显示“就绪”消息。用户报告发生了这种情况,但屏幕上的项目(如速度永远不会更新)。当应用程序保存它跟踪的位置时,没有任何内容。

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

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

任何人都可以看到可能导致问题的代码问题吗?

1 个答案:

答案 0 :(得分:0)

只是一个评论。

您的代码很紧凑,但不太可读。

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

我想知道你只是属于状态事件但不属于位置事件,但这是你的逻辑,如果对你来说没问题,那就好了。

Point是您想要在ui上显示的数据,只能分配给创建UI控件的线程中的ui控件,通常这是一个人们称之为“主”线程的线程。我不知道为什么这是主线程。事实上,我会称它为UI线程。

因此,您必须在创建UI控件的UI线程中传递数据。 所以éihter在UI线程上启动观察器,或者使用UI线程的Dispatcher将数据传递给它,并将数据分配给UI控件。