How to get the Geolocation in WP8?


In this blog post for WP8Dev, we will discuss how to retrieve the GeoCoordinates aka. Longitude and Latitude in Windows Phone 8 application. We will also discuss complete API usage. - Article authored by Kunal Chowdhury on .

If you are a WPDev and looking for a way to retrieve the Geo Location of the user and integrate that Geo Coordinates in your Windows Phone 8 application, this post will help you step-by-step to understand the API and the way to use it.

 

The Geolocator class provides us details about the user’s location. To use this class, you need to add proper capabilities to your application. Let’s start discussing on that.

 

Windows Phone 8 SDK provides few API classes, which you will need in order to play with the Geo Location coordinates in your WP8 application. Among them, few important classes are: Geolocator, Geoposition and Geocoordinate present under the Windows.Devices.Geolocation namespace.

 

Setting up the Environment

To begin with the integration of geolocation in your Windows Phone 8 application, the first thing that you must have to do is setting up the proper capabilities, failing which you might get ACCESS_DENIED exception in your application.

 

To add the capability in your application, open the WMAppManifest.xml file present under the Properties folder of your project and navigate to the “Capabilities” tab. Find out the ID_CAP_LOCATION capability from the list and make sure to check it as shown below to enable the Location Services in your app:

 

Windows Phone 8 Capabilities for Geo Location API

 

The second important thing (optional if you don’t want to test it in your physical WP8 device) that you need to configure is the Location service in your Windows Phone 8 device. If you want to play with the code directly in your device, navigate to settings –> location and make sure that the Location Service is enabled.

 

Understand the APIs

Once you setup your project with the proper capabilities, the next step is to play with the code. Before going thru the code implementation, let us first discuss on the API uses. The Geolocator class provides asynchronous API call GetGeopositionAsync() to initialize an asynchronous operation to retrieve the location (Geoposition) of the device.

 

Geoposition geoPosition = await new Geolocator().GetGeopositionAsync();

 

Geoposition class provides you two different property called CivicAddress and Coordinate to retrieve the civic address data and location coordinates associated with a geographic location respectively. CivicAddress provides you associated City, State, Country, PostalCode etc., whereas the Coordinate provides you the complete longitude and latitude of the accessed location. The later also provides you other different properties like Accuracy, Altitude, AltitudeAccuracy, Speed, PositionSource, SatelliteData etc. upon request.

 

Play with the Code

To find out the geo location of the user in your Windows Phone 8 application, you will need the help of both the Geolocator, Geoposition and Geocoordinate classes as described above. In this demo application, let us add two TextBox and one Button in the XAML page. Then register the click event of the button, so that, we can update the TextBox with the Geo Coordinates.

 

Here is the XAML code that we are going to use in our demo application:

 

<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel">
    <TextBlock Text="Latitude"/>
    <TextBox Text="{Binding Latitude}"/>
    <TextBlock Text="Longitude"/>
    <TextBox Text="{Binding Longitude}"/>
    <Button Content="Get Location" Click="OnGetLocationClicked"/>
</StackPanel>

 

As we said earlier that the Geolocator class exposes asynchronous method GetGeopositionAsync() and as we are going to use it in the click event of the button, let’s mark the event signature with the async keyword. Here is the complete C# code to get the geo location (Latitude and Longitude) of the user’s current location:

 

private async void OnGetLocationClicked(object sender, RoutedEventArgs e)
{
    try
    {
        var geoLocator = new Geolocator();
        var geoPosition = await geoLocator.GetGeopositionAsync();
        var geoCoordinate = geoPosition.Coordinate;
 
        Latitude = geoCoordinate.Latitude;
        Longitude = geoCoordinate.Longitude;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

 

Once you run the demo app now and click on the “Get Location” button, you will see a Location icon in the SysTray of your device as shown in the below screenshot (marked with Yellow arrow):

 

Windows Phone 8 Geo Location DemoWindows Phone 8 Geo Location Demo - Retrive the Geo Coordinates

 

When it successfully retrieves the Geocoordinates of the user, it will display proper Latitude and Longitude in the appropriate TextBox’es as shown above. Make sure that, if you are running the location APIs in your device, you have turned ON the Location Services.

 

I hope the explanation was useful for you to understand the Location APIs to get the current geo location of the user in your Windows Phone 8 application. In next post, we will try to discuss how to use the Geocoordinates in a map control and provide a proper mapped version of the location. Till then, enjoy reading my other articles in this blog. Have any question? Don’t forget to write a comment below.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.