Bing Map REST Service: Address GPS coordinate lookup with C#


Have you ever wondered how to look up latitude and longitud for your own address using Google or Bing Maps? I’m currently working with Microsoft Dynamics CRM where addresses is an important part of the sales and marketing processes. Therefore I wanted to be able to look up GPS coordinates for addresses like invoice, delivery and visiting. Last week I had some spare time to make deep dive into the world of Bing Maps REST service and the Location API.

After some testing of examples, I found out that the “Find a Location by Query” was the best way for me. I already had Bing Map key that is required for all requests against the Bing Map REST Service. The request URL that is sent to Bing Map REST Service consist of 5 parts:

  1. Base URL = “http://dev.virtualearth.net/REST/v1/Locations?”
  2. Actual search parameters = My address = “Ringstadåsen 39, Rolvsøy, Norge”
  3. Culture parameter = “nb-NO” (Norwegian)
  4. Output format = “xml”
  5. Include Neighbourhood (inclnb=1)
  6. Bing Map Key = “xxx” (where ‘xxx’ in my case is a 101 character long string).

URL after base URL:

culture=nb-NO&query=Ringstadåsen%2039%20Rolvsøy%20Norge&inclnb=1&output=xml&key=xxx

The actual address is located in the query parameter and spaces must be replaced with %20. This URL is executed through a standard HttpWebRequest and the result is given from a standard HttpWebResponse and delivered to caller in an XmlDocument.

public XmlDocument Execute(string url)
{
    var request = WebRequest.Create(url) as HttpWebRequest;
    var response = request.GetResponse() as HttpWebResponse;
    var xmlDoc = new XmlDocument();
    xmlDoc.Load(response.GetResponseStream());
    return (xmlDoc);
}

The returned XML is not complicated. Full XML is located on my dropbox. Here is the “main section” for my query above:

Bing_Location
I wrote some extension methods to retrieve values from the XmlDocument

public static class XmlExtensions
{
    public static string GetTag(this XmlDocument doc, 
                                XmlNamespaceManager nsmgr, string tag)
    {
        var path = string.Format(".//rest:{0}", tag);
        var entry = doc.SelectSingleNode(path, nsmgr);
        return entry != null ? entry.InnerText : null;
    }

    public static string GetTag(this XmlNode  node, 
                                XmlNamespaceManager nsmgr, string tag)
    {
        var path = string.Format(".//rest:{0}", tag);
        var entry = node.SelectSingleNode(path, nsmgr);
        return entry != null ? entry.InnerText : null;
    }
}

The first thing you need to is to retieve the status code from the XmlDocument. With the extension method listed above this is done by the code:

var lat = location.GetTag(nsmgr, "Latitude");
var lon = location.GetTag(nsmgr, "Longitude");

This were the main points from the source code.

Full source code: BingMap3.zip

Happy Bing’ing 🙂

Advertisements

6 thoughts on “Bing Map REST Service: Address GPS coordinate lookup with C#

  1. I want to select an area in bing map then I need to plot some pin on that boundary area. Can you please let me know how can I do that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s