Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current location using C#

My skill level: Beginner

Code: C# (wpf)

Hardware: Dell Venue 11 Pro tablet (windows 8.1)

I would like to get the current location of my computer via cellular triangulation in coordinates(latitude/longitude). Using the following link as a reference (Getting GPS coordinates on Windows phone 7), I have been attempting to pull the lat/lon coordinates from Windows Location Services. I have verified that Windows Location Services is receiving the coordinates via cell tower triangulation, but every time I run the following code the coordinates are listed as "NaN". Any help here would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Device.Location;

namespace GPS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);


        public MainWindow()
        {
            InitializeComponent();

            watcher.Start();

            LocationMessage(); 
        }



        private void LocationMessage()
        {

            var whereat = watcher.Position.Location;

            var Lat = whereat.Latitude.ToString("0.000000");
            var Lon = whereat.Longitude.ToString("0.000000");


            //optional parameters for future use
            whereat.Altitude.ToString();
            whereat.HorizontalAccuracy.ToString();
            whereat.VerticalAccuracy.ToString();
            whereat.Course.ToString();
            whereat.Speed.ToString();

            MessageBox.Show(string.Format("Lat: {0}\nLon: {1}",Lat,Lon)); 
        }


    }


}
like image 291
Dan Priest Avatar asked Aug 21 '15 18:08

Dan Priest


People also ask

What is Getcwd in C?

The getcwd() function shall place an absolute pathname of the current working directory in the array pointed to by buf, and return buf. The pathname copied to the array shall contain no components that are symbolic links. The size argument is the size in bytes of the character array pointed to by the buf argument.

What does chdir do in C?

chdir changes the current working directory of the calling process to the directory specified in path. Syntax: int chdir(const char *path); Parameter: Here, the path is the Directory path which the user want to make the current working directory.

What is $Get current directory?

The $GetCurrent folder is a hidden folder that is created in the root directory of the C drive during the upgrade process. The $GetCurrent folder contains log files about the last Windows 10 upgrade process, and may also contain the installation files for that update.

How do I get the current working directory in C#?

var currentDirectory = System. IO. Directory. GetCurrentDirectory();


1 Answers

If you only need your current position once, and not continous update as you move, you can replace Start with TryStart:

if (watcher.TryStart(false, TimeSpan.FromSeconds(3)))
{
    LocationMessage();
}
else
{
    // Position not found after 3 seconds...
}

Keep in mind that this method returns synchronously, so it is best not to run in on the UI thread.

like image 78
Frode Evensen Avatar answered Sep 19 '22 03:09

Frode Evensen