Question

I am trying to getting coordinates with the help of GPS and when i am putting a dry run or debug the event makes PositionChange & StatusChange to call TWICE. Here is my code please help me.

  private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (NetworkInterface.GetIsNetworkAvailable())
        {
            flag = true;
            if (watcher == null)
            {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                watcher.MovementThreshold = 20;
                watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
                watcher.Start();
            }
        }

    }


 void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
        switch (e.Status)
        {
            case GeoPositionStatus.Disabled:
                MessageBox.Show("Location Service is not enabled on the device");
                break;

            case GeoPositionStatus.NoData:
                MessageBox.Show(" The Location Service is working, but it cannot get location data.");
                break;
        }
    }

  void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        if (e.Position.Location.IsUnknown)
        {
            MessageBox.Show("Please wait while your prosition is determined....");
            return;
        }

        List<string> locationData = new List<string>();
        locationData.Add(e.Position.Location.Latitude.ToString("Latitude:" + "0.000"));
        locationData.Add(e.Position.Location.Longitude.ToString("Longitude:" + "0.000"));
        locationData.Add(e.Position.Location.Altitude.ToString());
        locationData.Add(e.Position.Location.Speed.ToString());
        locationData.Add(e.Position.Location.Course.ToString());
}
Was it helpful?

Solution

It's called twice because the status changes from

Initializing -> Ready

For Initializing it fires once and For ready the second time :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top