Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C# form application in which timer is set for every 1 second.

Within that I want to check the location of my device. Code is as follows:
This is how timer is called

this.Time1SecTimer = new System.Windows.Forms.Timer(this.components);
this.Time1SecTimer.Interval = 1000;
this.Time1SecTimer.Tick += new System.EventHandler(this.Time1SecTimer_Tick);
Time1SecTimer.Enabled = true;

//Time1SecTimer_Tick method
private void Time1SecTimer_Tick(object sender, EventArgs e) //Time1SecTimer Tick Event (every 1 sec)
       {
           double longitude = GetLongLatValue();
           this.LongitudeValueLabel.Text = longitude.ToString();
       }


//GetLongLatValue method
public double GetLongLatValue()
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

            GeoCoordinate coord = watcher.Position.Location;
            return coord.Longitude;

        }


Can anything be changed here for correct Longitude value?

What I have tried:

I have tried changing the GetLongLatValue method as follows but didn't work
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
            watcher.TryStart(false, // Do not suppress permissions prompt.
            TimeSpan.FromMilliseconds(1000)); // Wait 1000 ms to start.

            GeoCoordinate coord = watcher.Position.Location;
Posted
Updated 17-Jun-19 8:21am

1 solution

You're creating a new GeoCoordinateWatcher on every timer tick. DO NOT DO THAT!

You only one instance. So, create a class-level variable to hold the GeoCoordinateWatcher and use that to get the coordinate on every timer tick:
C#
public partial class Form1 : Form
{
    GeoCoordinateWatcher geo = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void StartButton_Click(object sender, EventArgs e)
    {
        geo = new GeoCoordinateWatcher();

        geo.TryStart(false, TimeSpan.FromSeconds(3));

        if (geo.Status == GeoPositionStatus.Ready)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }
    }

    private void Timer1_Tick(object sender, EventArgs e)
    {
        GeoCoordinate coord = geo.Position.Location;

        Log(coord.ToString());
    }

    private void Log(string message)
    {
        LogListBox.Items.Add(message);
    }
}

When your form closes, remember to call Dispose() on the watcher. I did not show that in the example.
 
Share this answer
 
Comments
Member 13459382 17-Jun-19 15:43pm    
Hello Dave,

Thanks for your answer. I tried this. Now the issue that I am having is that the status always gives NoData.

Do you know the reason for that?
Member 13459382 17-Jun-19 17:18pm    
I was making some mistake. Corrected it now. Its working. Thanks for the answer. It was really helpful.
[no name] 18-Jun-19 8:16am    
exactly what I was looking for, nice

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