:::: MENU ::::

Getting location details on UWP app

Hello all, In this post I will show you how to get current location details (Latitude and Longitude along with current address details like Country, town, street etc.). The API we are using for today are Windows.Devices.Geolocation and Windows.Services.Maps which is part of Windows Runtime APIs. Learn more about Windows Runtime APIs from here.

Now let’s see how to implement these APIs.

Important note: First check the Location capabilities in appxmanifest file.

Here is code to get current location (Latitude & Longitude):


First we need to get the permission to use location of the device. Geolocatior. RequestAccessAsync() helps us to get the permission from user. At first run of application it will show dialog box to get permission. Any response from user will fall under 3 Enum values of type GeoLocationAccessStatus i.e. Allowed, Denied and Unspecified. If response is allowed then it will continue to collect location data. We are using Geolocator class which provides access to the current geographic location. You can customize the location details by initializing properties of Geolocator object like DesiredAccuracy, DesiredAccuracyInMeters, MovementThreshold etc. Now get geographic position through await geolocator.GetGeopositionAsync() method. This method starts async operation to retrieve the current location of device and returns object of type GeoPosition which contains the location data like Latitude, Longitude, VenueData etc. Now we can parse the data from geoposition object in our case “pos”.

Now lets get the more details of our current location like country, town, street etc. For this purpose we are using MapLocationFinder class for which we have to add Windows.Services.Maps namespace.



As we can see Requesting permission to use device location is same as above so we are diving into MapLocationFinder class which provides methods to convert addresses to geographic locations(geocoding) and to convert geographic locations to addresses (reverse geocoding). For this sample we are using reverse geocoding part. You can also try geocoding feature where you can pass address (in string format and it will return you geographic locations).

So in our sample app I get my current location and create BasicGeoposition object with current Latitude and Longitude. And i am wraping BasicGeoposition object by GeoPoint class which is the format that MapLocationFinder class used to operate. Now Pass GeoPoint object as a parameter for FindLocationAtAsync() method of MapLocationFinder Class which converts a geographic location to a collection of addresses. Then FindLocationAtAsync() returns MapLocationFinderResult object which holds the result of MapLocationFinder query. Now check the status of result if it is Success then we are OK to access the data like country name, country code etc. from “result” object else display message dialog with an error message.

Here is output.

That’s it for this post. You can get samples from my github.

Happy Coding!!! 🙂