Find Latitude and Longitude of a System C#
I'm share the topic of how to find out Latitude and longitude of
a system by win form in C#.
Some points here:
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.
C#(Code).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Device.Location;
namespace
Find_Latitude_and_Longitude_of_a_System
{
public
partial class Form1 : Form
{
private
string latitude;
private
string longitute;
private
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
watcher = new GeoCoordinateWatcher();
// Catch the StatusChanged event.
watcher.StatusChanged +=
Watcher_StatusChanged;
// Start the watcher.
watcher.Start();
}
private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs
e) // Find GeoLocation of Device
{
try
{
if (e.Status == GeoPositionStatus.Ready)
{
// Display the latitude and
longitude.
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 button1_Click(object sender, EventArgs e)
{
textBox1.Text = latitude;
textBox2.Text = longitute;
}
}
}
Comments
Post a Comment