Get location coordinates with windows phone application
Tags: windowsphone, app, geolocation, mobileapp
Published: 2018-02-10 22:31:37

---
title: "Get location coordinates with windows phone application"
published: true
tags: Windows Phone,app,geo location,mobile app
canonical_url: http://blog.mandarbadve.com/2015/09/02/get-location-coordinates-with-windows-phone-application/
---

_This post was originally published on [blog.mandarbadve.com](http://blog.mandarbadve.com/2015/09/02/get-location-coordinates-with-windows-phone-application/)_


### Location API

You can use Windows Phone’s location API if your windows phone application needs location of user. In this post we will see how to use location API.

### Let’s code

Getting started by creating new windows phone application.

[![CreateProject](https://i0.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/CreateProject.png?resize=656%2C455)](https://i0.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/CreateProject.png)

Double click on ‘WMAppManifest.xml’ file.(You can read [here](https://msdn.microsoft.com/en-us/library/windows/apps/ff769509(v=vs.105).aspx) for more details.) You will find this file inside projects Properties.

[![Capabilities](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/Capabilities.png?resize=319%2C264)](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/Capabilities.png)

Go to&nbsp;Capabilities tab and then select ‘ID\_CAP\_LOCATION’ capability. This will tell to Windows Phone Store that this application need access to users location. While installing application user will see prompt message about the need of location service. This is the first step of get permission to access location from the user.

[![LocationServiceCapabilities](https://i2.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/LocationServiceCapabilities.png?resize=541%2C583)](https://i2.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/LocationServiceCapabilities.png)

We need some user interface to show location status. Copy the following code block inside the default grid created by visual studio template.

### XAML Code

```
<StackPanel> 
    <Button x:Name="btnGetLocation" Click="btnGetLocation_Click">Get Location</Button> 
    <TextBlock x:Name="txtLatitude"></TextBlock> 
    <TextBlock x:Name="txtLongitude"></TextBlock> 
    <TextBlock x:Name="txtStatus"></TextBlock> 
</StackPanel>
```

Finally your XAML code and designer will looks like this.

[![XAMLCode](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/XAMLCode.png?resize=656%2C335)](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/XAMLCode.png)

### Code behind

Go to MainPage.xaml.cs and add following method

```
protected override void OnNavigatedTo(NavigationEventArgs e) 
 { 
 if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationPermission")) 
 { 
 //// User already allowed app to access location 
 return; 
 }
 
 //// User not allowed app to access location 
 //// Ask user to allow

 MessageBoxResult result = 
 MessageBox.Show("Application needs your location. Please allow.", "Location", MessageBoxButton.OKCancel);
 
 if (result == MessageBoxResult.OK) 
 { 
   //// If user allows to access location 
   IsolatedStorageSettings.ApplicationSettings["LocationPermission"] = true; 
 } 
 else 
 { 
    //// If user not allows to access location 
    IsolatedStorageSettings.ApplicationSettings["LocationPermission"] = false; 
 } 
 IsolatedStorageSettings.ApplicationSettings.Save(); 
}
```

User will navigate to the MainPage.xaml page when he open application. At this time&nbsp;OnNaviatedTo method get called. This method first check user already set permission to access location.(No check here if he accepted or rejected location access request. We will check it later).

If user has not given permission to access location, we will show Ok Cancel message box of asking the permission to access location. If he responds with Ok, we will store the value of ‘LocationPermission’ key as true else false if user select Cancel option.

[![OnNevigaetedTo](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/OnNevigaetedTo.png?resize=480%2C800)](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/OnNevigaetedTo.png)

### Get Location

Below is the button click event handler. Copy code and paste it below to above method.

```
private async void btnGetLocation_Click(object sender, RoutedEventArgs e) 
 { 
 if (!(bool)IsolatedStorageSettings.ApplicationSettings["LocationPermission"]) 
  { 
    return; 
  }
 Geolocator geolocator = new Geolocator(); 
 geolocator.DesiredAccuracyInMeters = 50;

 try 
  { 
   Geoposition geoposition = await geolocator.GetGeopositionAsync( 
   maximumAge: TimeSpan.FromMinutes(10), 
   timeout: TimeSpan.FromSeconds(10) 
   );

   txtLatitude.Text = "Latitude: " + geoposition.Coordinate.Latitude.ToString(); 

   txtLongitude.Text = "Longitude: " + geoposition.Coordinate.Longitude.ToString(); 

  } 
  catch (Exception) 
  { 
    txtStatus.Text = "Your phone may not have geolocation API support."; 
  } 
 }
```

This method first check weather user allowed to access location or not by accessing the value of key ‘LocationPermission’ from Application Settings. If user not allowed then we will simply return.(You can show message to user here. E.g. You have rejected permission to access location.)

If user allows, we will create object of Geolocator. Set property ‘DesiredAccuracyInMeters’ to 50 meters. This option defines the accuracy of the location. You can get object of Geopostion by using the GetGeopositionAsync method of&nbsp;Geolocator&nbsp;object. Geoposition object contain Coordinate property which have Latitude and Longitude values. Set this values to the label as text.

Here is the final screen where you will see the Latitude and Longitude values on screen.

[![Result](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/Result.png?resize=480%2C800)](https://i1.wp.com/blog.mandarbadve.com/wp-content/uploads/2015/09/Result.png)

### Conclusion

In this post we have seen the following things

- How to add location API capability in the manifest file.
- Ask user to allow to access location.
- Save user response in application settings.

In the next post we will see how can we use this coordinates to show location on maps.