Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Watcher = new GeoCoordinateWatcher();
// Start the watcher.
if (Watcher.TryStart(false, TimeSpan.FromSeconds(3)))
{
    MessageBox.Show(Watcher.Position.Location.ToString()); //Always 'Unknow'
}
else
{
    // Position not found after 3 seconds...
}


What I have tried:

C#
Watcher = new GeoCoordinateWatcher();
// Start the watcher.
if (Watcher.TryStart(false, TimeSpan.FromSeconds(3)))
{
    MessageBox.Show(Watcher.Position.Location.ToString()); //Always 'Unknow'
}
else
{
    // Position not found after 3 seconds...
}
Posted
Updated 21-Nov-21 2:51am
Comments
phil.o 18-Nov-21 7:15am    
You should check the provider, as it seems that no data can be retrieved.

1 solution

You appear to be using it wrong. Furthermore, you can't use MessageBox to display the position because you'll be forced to keep hitting OK to dismiss the dialog.

private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
private bool watcherStarted = false;

public void MakeCoordWatcher(int seconds)
{
    if (!eventWatched)
    {
        this.watcherwatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
        this.watcherStarted = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(seconds*1000));
        if (!started)
        {
            MessageBox.Show("GeoCoordinateWatcher timed out on start.");
        }
    }
}

private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    // Do something with the position data
    Debug.WriteLine("Lat: {0}, Long {1}", e.Position.Location.Latitude, e.Position.Location.Longitude);
}


How you display the watcher updates is up to you, and since you're using MessageBox, I'm assuming the app has a UI, so I would probably create a status bar that is updated by the event handler. Since I have no idea how your app is architechted, I leave that decision up to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900