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:
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 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:
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:
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):
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.