Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Geolocation in Windows Phone 8

0.00/5 (No votes)
2 Dec 2013 1  
This article shows you how to use the Geolocation feature in Windows Phone 8.

Introduction

This article shows you how to use the Geolocation feature in Windows Phone 8.

Background

Before starting coding, we need to define the geolocation and I find the best Definition of this notion is the identification of the location of an object on a Map or a Plan. This position is defined by some properties like the Longitude and the Latitude. On Windows Phone SDK, the coordinates are represented by an object called GeoCoordinates.

To use this feature, you need to add the manifest capacity: ID_CAP_LOCATION.

Using the code

In order to retrieve the current position, you need to add the following code that return an object called position that includes two essential properties CivicAdress and Coordinate:
 

var locator = new Geolocator();
var position = await locator.GetGeopositionAsync(); 

GeoCoding:

Geocoding is the process to obtain the Geographical Coordinates from an address. It is very useful because almost of systems use APIs as points to be able to search and be able to sort by proximity.

This is an example of a method of geocoding that retrieve only a list of positions :

public  async Task<IEnumerable<GeoCoordinate>> Geocode(string address)
{
    var tcs = new TaskCompletionSource<IEnumerable<GeoCoordinate>>();

    var query = new GeocodeQuery { SearchTerm = address };

    EventHandler<QueryCompletedEventArgs<IList<MapLocation>>> queryQueryCompleted = null;
    queryQueryCompleted += (s, a) =>
    {
        query.QueryCompleted -= queryQueryCompleted;
        var locations = a.Result.Select(location => location.GeoCoordinate).ToList();
        tcs.SetResult(locations);
    };

    query.QueryCompleted += queryQueryCompleted;
    query.QueryAsync();
    return await tcs.Task;
}

The code isn't clear ? don't worry I'm going to specify it. So, to geocode, I need to create a geocoding request that specify the end of my research, then I run my search and the request is asynchronous and the results are reported in the event QueryCompeted.

Reverse Geocoding

It is the reverse of Geocoding. It means to identify the coordinates giving them a physical Adress(civic). For example, it is clearer to say that I'm in Teniour Street Km 4 Sfax than saying I'm in Latitude: 34.745 and the Longitude: 10.761.

Here is an example about this process:

public  async Task<List<string>> ReverseGeocode(double latitude, double longitude)
{
    var tcs = new TaskCompletionSource<List<string>>();

    var query = new ReverseGeocodeQuery();
    query.GeoCoordinate = new GeoCoordinate(latitude, longitude);
    EventHandler<QueryCompletedEventArgs<IList<MapLocation>>> queryQueryCompleted = null;
    queryQueryCompleted += (s, a) =>
    {
        query.QueryCompleted -= queryQueryCompleted;
         
        var mapAddresses = a.Result.Select(location => 
                 GetAddressLine(location.Information.Address)).ToList();
        tcs.SetResult(mapAddresses);
    };

    query.QueryCompleted += queryQueryCompleted;
    query.QueryAsync();
     
    return await tcs.Task;
}

GetAdressLine is a simple method that I created to retrieve the formatted address:

public string GetAddressLine(MapAddress mapAddress)
{
  return string.Format(“{0},{1},{2}”,
  mapAddress.Street,
  mapAddress.PostalCode,
  mapAddress.Country);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here