Home / C#.NET / C#.NET Getting Current Location and Implementing Reverse Geocoding
GeoHeadingSmall

C#.NET Getting Current Location and Implementing Reverse Geocoding

Hello everyone.  Today I’m going to do a sample program to get user’s current location (longitude, latitude) and using that i’m going to do a reverse geocoding using Google Location REST service and get all the current location details of the user.

This will be much useful when you develop windows apps / WPF apps to get user’s current location. Even in a centralized server you can implement this.

This method will work in Windows 8 , Windows Server 2012 and above.

I’m using a Windows 10 laptop to perform this exercise.

First of all , you need to enable the location service in your laptop or the server. Please follow the below steps to enable location service.

Settings -> Privacy -> Location -> Then turn on the App Connector.

Windows10Location

Okay now your location service is enabled for the applications. In C# you have GeoCoordinateWatcher to get Location details. Now Lets see how we can use GeoCoordinateWatcher in the code.

GeoCoordinateWatcher comes in System.Device.Location. So don’t forget to add it.

 
    private void GetLocation()
        {
            GeoCoordinateWatcher geowatcher = null;
            try
            {
                geowatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                geowatcher.StatusChanged += Geowatcher_StatusChanged;
                geowatcher.Start();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

GeoPositionAccuracy is used to set the accuracy level of the location. You can “High” but it will affect the performance.

Then we create an event using a delegate for StatusChanged (geowatcher.StatusChanged += Geowatcher_StatusChanged;). Please find the below StatenChanged Method.

 private void Geowatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
 {
   try
   {
     if (e.Status == GeoPositionStatus.Ready)
     {
       var location = geowatcher.Position;
       geowatcher.Stop();
     }
   }
   catch (Exception ex)
   {
       throw ex;
   }
   finally
   {

   }
 }

If you notice, I have started the watcher in the first method and stopped once the status is “Ready” in the StatusChanged Event. Until you stop the watcher, it will keep on calling the status change event. If you to keep it running, you can do that also.

Now you have the longitude and latitude in the Location Object (var Location) which was taken from geowatcher.Position . Using these inputs you can now do the reverse geocording(Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. ) and get the actual name of the current location including other details.

In order to do the reverse geocoding you need to use a Google Rest API. Please refer to the below.

http://maps.googleapis.com/maps/api/geocode/json?latlng=” [LATITUDE] “,”[LONGITUDE] “&sensor=false”

you have to pass the Latitude and longitude parameters which you found in the geowatcher.Position in the above code.

Once you call the Google geocode api, you will get the JSON output with all the details about the current location in readable format.

you can call the REST service using a simple webclient.

So this is how you get the Current Location using C# GeoCoordinateWatcher and implement a reverse geocoding.

If you have any questions , please feel free to contact us.

Thanks.

About Kasun Koswattha

Kasun Koswattha
Kasun Koswattha (Kasun) a .NET Engineer | a Gamer from United States. Join with the c2k community to share your knowledge. Like and share...

Check Also

C#.NET - MS SQL Server 2014 Installation - සරලව සිංහලෙන් - 05

ආයුබෝවන් හැමෝටම! පහුගිය සතියෙ වීඩියෝව ගේන්න බැරි උනාට සමාවෙන්න ඕනේ. වැඩත් එක්ක පොඩ්ඩක් කාර්යබහුල උනා. …

Leave a Reply