Click here to Skip to main content
15,886,872 members
Articles / Mobile Apps / Windows Mobile
Tip/Trick

Geolocation in Windows Phone 8

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
2 Dec 2013CPOL1 min read 13.3K   2  
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:
 

C#
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 :

C#
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:

C#
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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Tunisia Tunisia
Microsoft Certified Professional, Big Data and Cloud Architect.
Mail : hadrichmed@gmail.com

Comments and Discussions

 
-- There are no messages in this forum --