WP7: Dissecting we.Muslim app | Part 6 – Get Area Name from Latitude Longitude

Given a latitude longitude coordinate, let’s now resolve this to a meaningful Area, Country string.

Previous parts in this series:

  1. Part 1 – Wireframe
  2. Part 2 – Composites
  3. Part 3 – Icon Sizes
  4. Part 4 – Proper Splash Screen
  5. Part 5 - Get Current Latitude Longitude

In Part 5 we were able to use Location Services to give us the current coordinate in Latitude Longitude. This information is useful for calculating Prayer Times, but I wouldn’t say “The prayer times for Lat 24.47, Long 54.37 are…” Instead I would like to say “The prayer times for Abu Dhabi are…”

So how do we do that? What we are doing is called Reverse GeoCoding : given a Latitude-Longitude pair, resolve this to a meaningful address.

Obviously to do this, we need help from a 3rd party service, and luckily, Bing has such GeoCoding service available for us to use freely.

Get a Bing API Key

  1. Browse to http://www.bingmapsportal.com
  2. Click Create to create a new account
  3. Type in your Account Details
  4. Click Create or View Keys
  5. Type in your app name & URL, then click Create Key
    - http://CompanyName if you have a company website
    - http://localhost if you don’t have a company website


Add Service Reference to Reverse GeoCoding Service

Right click your project and choose Add Service Reference, point it to this URL: http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc



ReverseGeocoder Class Wrapper

And here is my code for the wrapper, following the Single-Responsibility Principle (SRP) of SOLID:

using System.Device.Location;
using System.Threading;
using we.Muslim.BingGeocode;
using we.Muslim.Models;
namespace we.Muslim.Utils
  public enum ReverseGeocoderEnum
  public class ReverseGeocoder
    private readonly ManualResetEvent _syncGeocode = new ManualResetEvent(false);
    private string _result;
    public string GetAreaAndCountry(GeoCoordinate coordinate)
      var req = CreateRequest(coordinate);
      return ExecuteRequest(req, ReverseGeocoderEnum.AreaAndCountry);
    private string ExecuteRequest(ReverseGeocodeRequest req, ReverseGeocoderEnum partsToGet)
      var client = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
      client.ReverseGeocodeCompleted += client_ReverseGeocodeCompleted;
      client.ReverseGeocodeAsync(req, partsToGet);
      // Wait until we get a name for the coord
      return _result;
    private static ReverseGeocodeRequest CreateRequest(GeoCoordinate coordinate)
      var req = new ReverseGeocodeRequest();
      req.Credentials = new Credentials {ApplicationId = SharedData.BING_KEY};
      var loc = new Location
                        Latitude = coordinate.Latitude,
                        Longitude = coordinate.Longitude
      req.Location = loc;
      return req;
    private void client_ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
      var resp = e.Result;
      if (resp.Results.Count > 0)
        var partsToGet = (ReverseGeocoderEnum) e.UserState;
        switch (partsToGet)
          case ReverseGeocoderEnum.AreaAndCountry:
            var local = resp.Results[0].Address.Locality;
            var country = resp.Results[0].Address.CountryRegion;
            _result = string.Format("{0}, {1}", local, country);
            _result = resp.Results[0].DisplayName;
      // Tell the other thread that name is set

Now you just need to modify the Enums above to customize the address string returned from the ReverseGeocoder service.

And this is how you use the above code:

  // 1. Test Retriever 
  var retriever = new GeoCoordRetriever();
  var coord = retriever.ForCurrentLocation();
  if (coord == null) return null;
  // 2. Test Reverser
  var resolver = new ReverseGeocoder();
  var name = resolver.GetAreaAndCountry(coord);
  if (string.IsNullOrEmpty(name)) return null;


Now enter this coord in the GPS Emulator: Latitude -6.17 and Longitude 106.83.

What do you get? Well you should get location name as screenshot below:


Share this post: | | | |
Published Sunday, February 20, 2011 8:30 PM by zeddy
Filed under:


# Twitter Trackbacks for WP7: Dissecting we.Muslim app | Part 6 ??? Get Area Name from Latitude Longitude - Z & his Startup [netindonesia.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 WP7: Dissecting we.Muslim app | Part 6 ??? Get Area Name from Latitude Longitude - Z & his Startup         [netindonesia.net]        on Topsy.com

Powered by Community Server (Commercial Edition), by Telligent Systems