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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn about the Win32 Error “0x000019E5 – ERROR_COULD_NOT_RESIZE_LOG” that you get when debugging system erors in...
In this post, you’ll learn about the error “CO_E_DBERROR 0x8004E02B” that is returned when working with COM based APIs or...
In this post, you’ll learn about the Win32 Error “0x000019D0 – ERROR_LOG_BLOCK_VERSION” that you get when debugging system erors in...