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.
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.
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();
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.