Search code examples
windows-phone-8windows-phone-8-emulator

Geolocator position changed event


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??


Solution

  • 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