question

anilkumar-7193 avatar image
0 Votes"
anilkumar-7193 asked HuiLiu-MSFT commented

Current Location Using window Application WPF

I am working on Wpf application and want to get latitude and longitude(EXACT) of desktop when mark attendance
and save them to database.Can anyone help for the same
Thanks in Advance

dotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT commented

You could refer to the following methods to get the latitude and longitude and save it to the database.
The code of xaml:

 <StackPanel>
         <TextBox Name="txtId" Width="200" Height="30"></TextBox>
         <TextBox Name="txtLatitude" Width="200" Height="30"></TextBox>
         <TextBox Name="txtLongitude" Width="200" Height="30"></TextBox>
         <Button Name="load" Click="load_Click">load</Button>
     </StackPanel>

The code of xaml.cs:

 public partial class MainWindow : Window
   {
     public MainWindow()
     {
       InitializeComponent();
       GetLocationEvent();
     }
     GeoCoordinateWatcher watcher;
     public void GetLocationEvent()
     {
       this.watcher = new GeoCoordinateWatcher();
       this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
       bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(20000));
          
     }
     void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
     {
       PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);
     }
     void PrintPosition(double Latitude, double Longitude)
     {
       MessageBox.Show("latitude" + Latitude + "longitude" + Longitude);
       txtLatitude.Text = Latitude.ToString();
       txtLongitude.Text = Longitude.ToString();
     }
    
     private void load_Click(object sender, RoutedEventArgs e)
     {
       SqlConnection con = new SqlConnection(@"constr");
       SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[LocationTable]([Id],[Latitude],[Longitude])VALUES('" + int.Parse(txtId.Text) + "','" + float.Parse(txtLatitude.Text)+"','"+float.Parse(txtLongitude.Text)+"')",con); 
       con.Open();
       cmd.ExecuteNonQuery();
       MessageBox.Show("Data created successfully");
       con.Close();
     }
   }

The picture of result:
134267-2.gif


If the response is helpful, please click "Accept Answer" and upvote it.
 Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


2.gif (112.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for answer
In this how to ensure that watched longitude and latitude is exactly matched on google map with address

0 Votes 0 ·

Hi,@anilkumar-7193. I tested and found that my location is also nearly 30 different from Google Maps( Latitude: 31.329601 Longitude: 120.434238). It may be because there is no gps sensor. You could try to refer here.

0 Votes 0 ·