Floating FB popout byF5debug

Programmatically get User Location in Windows Store Apps

 

In this tutorial you are going to see how to programmatically get the location information of the device in a windows store application. There are scenarios where we need to collect the user information for audit or to understand where the user currently locate to provide a better information on their needs near by. So what we can get with the location information, basically we can get the Latitude and Longitude of the current location from which the app has been launched and event triggered.

To access the location information from the Windows store app we need to access using the GeoLocator class which has the property to get us the information of Latitude and Longitude of the specific location. Ok Lets see the code, before starting first step is to enable the capabilities for the application to access the location. To do that go to the Solution Explorer of the application and open the Package.appxmanifest file and navigate to the Capabilities tab to activate Location capability as shown in the screen below.

image

Now from the code, access the Geolocator class and create an instance with the instance you can get the desired location by accessing the GetGeoPositioAsyn method and get the position information which is basically Latitude and Longitude information of the location. Geoposition will get the accurate information unless providing the Accuracy to High or Default.

Code:

private async void Button_Click(object sender, RoutedEventArgs e)
       {
           var geoLoc = new Geolocator();

           geoLoc.DesiredAccuracy = PositionAccuracy.High;
           Geoposition pos = await geoLoc.GetGeopositionAsync();
           txt1.Text = "Latitude: " + pos.Coordinate.Latitude.ToString();
           txt2.Text = "Longitude: " + pos.Coordinate.Longitude.ToString();
          
       }

 

image

Now build and execute the application, first the app will request the user for permission to get the location enabled in the device. Upon acceptance you can get the location information (Latitude and Longitude) as shown in the screens below.

image

image

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter