Question

How exactly do you use the BING REST api (specifically the ROUTES part) to get a driving distance in ASP.NET.

I have searched high and low on Google for this answer and none is forthcoming.

I have found url strings such as:

http://dev.virtualearth.net/REST/v1/Routes/Driving?waypoint.0=redmond&heading=90&waypoint.1=seattle&du=mi&key=BingMapsKey

That's great! But how to call it from ASP?

I have also found this code:

 private void GetResponse(Uri uri, Action<HttpResponse> callback)
    {
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += (o, a) =>
        {
            if (callback != null)
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(HttpResponse));
                callback(ser.ReadObject(a.Result) as HttpResponse);
            }
        };
        wc.OpenReadAsync(uri);
    }

Which is a "generic method to make web requests". But, again, how do you call it? I find it confusing that it doesn't require a return type.

In order to call it, I have found code like this:

string key = "YOUR_BING_MAPS_KEY or SESSION_KEY";
    string query = "1 Microsoft Way, Redmond, WA";

    Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
    GetResponse(geocodeRequest, (x) =>
    {
        Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
        Console.ReadLine();
    });

But when I add this to the project, I get every error under the sun coming up. So, I am stuck.

I am a total ASP beginner and haven't found any online documentation any help at all.

p.s. I do have a BING api key and do use it in the code above.

Was it helpful?

Solution

I am not an expert in this, but the below compiles for me. Also make sure to add the data constructs as mentioned in the BING documentation:

protected void Page_Load(object sender, EventArgs e)
{
    string key = "YOUR KEY";
    string query = "ADDRESS";

    Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));

    GetResponse(geocodeRequest, (x) =>
    {
        Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
        Console.ReadLine();
    });

}

Quoting from another stackoverflow question:


The bottom of the documentation you are using points to the Data contracts you need for the REST services which are available here: http://msdn.microsoft.com/en-us/library/jj870778.aspx

Simply create a empty C# file and copy in paste in the C# Data Contracts. Then add the namespace to this class:

using BingMapsRESTService.Common.JSON;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top