Question

Vladyslav Koshyl on Fri, 19 Feb 2016 23:08:11


Hi all! I'm making a simple app for my WP8.1 device, which will be track my maximum speed. I'm using System.Device.Location.GeoCoordinateWatcher for this. I can detect my position, but speed always is NaN. And I don't understand, why. What is wrong? Thanks for any help or info. It is my full code below:

namespace SpeedTracker
    {
        public partial class MainPage : PhoneApplicationPage
        {
            GeoCoordinateWatcher watcher;
            double maxSpeed = 0.0;

        public MainPage()
        {
            InitializeComponent();            
        }

        private void StartTrackingBtn_Click(object sender, RoutedEventArgs e)
        {
            this.watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);

            this.watcher.MovementThreshold = 10;
            this.watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

            this.watcher.Start();
        }

        private void StopTrackingBtn_Click(object sender, RoutedEventArgs e)
        {
            this.watcher.StatusChanged -= new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            this.watcher.PositionChanged -= new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

            this.watcher.Stop();
        }

        private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (this.watcher.Position.Location.IsUnknown != true)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.maxSpeed = Math.Max(this.maxSpeed, e.Position.Location.Speed);
                    this.SpeedValueTxblck.Text = this.maxSpeed.ToString();
                });
            }
            else
            {
                this.SpeedValueTxblck.Text = "Please wait while your prosition is determined...";
            }
        }

        private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        this.SpeedValueTxblck.Text = "Location Service is not enabled on the device";
                    });
                    break;

                case GeoPositionStatus.NoData:
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        this.SpeedValueTxblck.Text = "The Location Service is working, but it cannot get location data";
                    });
                    break;
                default:
                    break;
            }
        }

        private void GetLocationCourseAndSpeed()
        {
            this.watcher.TryStart(true, TimeSpan.FromMilliseconds(1000));

            if (watcher.Position.Location.IsUnknown != true)
            {
                GeoCoordinate coord = watcher.Position.Location;

                this.maxSpeed = Math.Max(this.maxSpeed, coord.Speed);

                this.SpeedValueTxblck.Text = this.maxSpeed.ToString();
            }
            else
            {
                this.SpeedValueTxblck.Text = "Unknown";
            }
        }
    }
}



Sponsored



Replies

SeanMcDev on Wed, 18 May 2016 22:20:19


Wrong forum.