Current Location Using window Application WPF

anil kumar 61 Reputation points
2021-09-20T08:30:45.653+00:00

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

Developer technologies | XAML
Developer technologies | XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2021-09-22T09:41:52.73+00:00

    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][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.