I'm share the topic of how to find out Latitude and longitude of a system by win form in C#.
Some points here:
- This only works in .NET Framework 4.0 or later and Windows 7 or later.
- The Location API is defined in the System.Device DLL so add a reference to that library. The code includes the following using directive to make using the Location API easier.
- using System.Device.Location;
- private string latitude;
- private string longitute;
- private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.Text = latitude;
- textBox2.Text = longitute;
- }
- private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
- {
- try
- {
- if (e.Status == GeoPositionStatus.Ready)
- {
-
- if (watcher.Position.Location.IsUnknown)
- {
- latitude = "0";
- longitute = "0";
- }
- else
- {
- latitude = watcher.Position.Location.Latitude.ToString();
- longitute = watcher.Position.Location.Longitude.ToString();
- }
- }
- else
- {
- latitude = "0";
- longitute = "0";
- }
- }
- catch (Exception)
- {
- latitude = "0";
- longitute = "0";
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- watcher = new GeoCoordinateWatcher();
-
- watcher.StatusChanged += Watcher_StatusChanged;
-
- watcher.Start();
- }
- }
The accuracy of the location depends on the data available to the computer. If the computer has GPS (many phones and tablets do), this may be quite accurate. If the computer doesn’t have GPS (most desktop systems don’t have it), the API may use wi-fi triangulation or some other method, which may be less accurate.