How to Get the Current Location in Windows Phone 8?

This article will explain how to quickly get the current location in Windows Phone 8 using Geolocator class.

The Windows Phone 8 includes a new geographic location API which provides the Geolocator class to retrieve the current location.

In the previous version of Windows Phone (Windows Phone 7.5), we used the GeoCoordinateWatcher class to retrieve the location. Although this is still supported, it is recommended to use the new Geolocator class especially when your targets the Windows Phone 8 device.

How to Get the Current Location in Windows Phone 8?

The Geolocator class can be used to get the location / track the location of the Windows Phone. The Geolocator class also contains events like PositionChanged and Status Changed to track the location. In this blog post, let’s try to get the current location without tracking it via events.

The Geolocator class includes the method GetGeopositionAsync which retrieves the current location of the phone. One of the advantages of the Geolocator and the GetGeopositionAsync method is that you dont need to subscribe to the PositionChanged event when all you need is to get the location only once.

The Geolocator class is defined in the namespace Windows.Devices.Geolocation. Note that you must use the await and async keywork when trying to access the GetGeopositionAsync.

async private void Button_Click_1(object sender, RoutedEventArgs e)

{

Geolocator locator = new Geolocator();

Geoposition position = await locator.GetGeopositionAsync();

Geocoordinate coordinate = position.Coordinate;

string Location = "Latitude = " + coordinate.Latitude + " Longitude = " + coordinate.Longitude;

MessageBox.Show(Location);

}

How to Get the Current Location in Windows Phone 8?

Note that you must include the Location capabilities in your Application Manifest file when you want to use the Geolocator in your application.

No Responses

  1. How to Get the Current Location in Windows Phone 8? | Program In .NET July 21, 2013 Reply

Leave a Reply