none
How can I use direction with Bing maps ?

    Question

  • Hi, I have two text box, one for from location and another for destination location. User's co-ordinate position or map tap result or location as text will be parameter for those text boxes, So how can I get direction from one location to other in my c#/xaml metro app ?
    Monday, September 17, 2012 9:26 AM

Answers

  • You have to use the Bing Maps REST Routing service to calculate a route. take a look at this blog post to see how to use this service from .NET: http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/ If you are using the JavaScript API then use the directions Module.

    http://rbrundritt.wordpress.com

    • Marked as answer by Xyroid Tuesday, September 18, 2012 9:36 AM
    Monday, September 17, 2012 9:32 AM
  • I can't provide full source code. However here is how you implement the REST services in a Metro app.

    In your Metro application create a new C# file. Open it up and delete the contents then copy and paste the Data Contract in that file.

    For Metro apps you can use the following method to get a response from the service:

    private async Task<Response> GetResponse(Uri uri)
    {
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        var response = await client.GetAsync(uri);
        using (var stream = await response.Content.ReadAsStreamAsync())
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
            return ser.ReadObject(stream) as Response;
        }
    }

    You can call the service like this:

    //Create the request URL for the Geocoding service
    Uri geocodeRequest = new Uri(
        string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, MyMap.Credentials));
    //Make a request and get the response
    Response r = await GetResponse(geocodeRequest);

    http://rbrundritt.wordpress.com

    Monday, September 17, 2012 1:05 PM
  • To draw the route line on the map you will need to take the route points and create a LocationCollection out of it. Then use this to create a MapPolyline object and add that to the map. You can then use the start and end points of the map to add start and end pushpins. As for the itinerary you will need to render that yourself. Try using a ListBox and binding to the different properties of the route.

    http://rbrundritt.wordpress.com

    • Marked as answer by Xyroid Tuesday, September 18, 2012 9:36 AM
    Tuesday, September 18, 2012 9:09 AM
  • Finally I managed to do, here is my blog post and also CodeProject article with demo, it will help all others. Thanks Richard
    • Marked as answer by Xyroid Tuesday, September 18, 2012 9:36 AM
    • Edited by Xyroid Wednesday, September 19, 2012 7:11 AM
    Tuesday, September 18, 2012 9:36 AM

All replies

  • You have to use the Bing Maps REST Routing service to calculate a route. take a look at this blog post to see how to use this service from .NET: http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/ If you are using the JavaScript API then use the directions Module.

    http://rbrundritt.wordpress.com

    • Marked as answer by Xyroid Tuesday, September 18, 2012 9:36 AM
    Monday, September 17, 2012 9:32 AM
  • Thanks Richard, can you please give me full source of that silverlight project or guide me how can I implement this REST service in Metro ? How can I use that class and what method parameters and all. 
    Monday, September 17, 2012 10:14 AM
  • I can't provide full source code. However here is how you implement the REST services in a Metro app.

    In your Metro application create a new C# file. Open it up and delete the contents then copy and paste the Data Contract in that file.

    For Metro apps you can use the following method to get a response from the service:

    private async Task<Response> GetResponse(Uri uri)
    {
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        var response = await client.GetAsync(uri);
        using (var stream = await response.Content.ReadAsStreamAsync())
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
            return ser.ReadObject(stream) as Response;
        }
    }

    You can call the service like this:

    //Create the request URL for the Geocoding service
    Uri geocodeRequest = new Uri(
        string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, MyMap.Credentials));
    //Make a request and get the response
    Response r = await GetResponse(geocodeRequest);

    http://rbrundritt.wordpress.com

    Monday, September 17, 2012 1:05 PM
  • I want to draw driving route on map, though I got the link of that it is like this

    http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&wp.0=44.979035,-93.26493&wp.1=34.943828508257866,-93.09332862496376&optmz=distance&rpo=Points&key=MyKey

    Now can I draw on map ???

    I have found this (hat I want) in JavaScript I want the same in C#/XAML. Can you please guide me ????

    • Edited by Xyroid Tuesday, September 18, 2012 7:20 AM
    Tuesday, September 18, 2012 5:19 AM
  • To draw the route line on the map you will need to take the route points and create a LocationCollection out of it. Then use this to create a MapPolyline object and add that to the map. You can then use the start and end points of the map to add start and end pushpins. As for the itinerary you will need to render that yourself. Try using a ListBox and binding to the different properties of the route.

    http://rbrundritt.wordpress.com

    • Marked as answer by Xyroid Tuesday, September 18, 2012 9:36 AM
    Tuesday, September 18, 2012 9:09 AM
  • Finally I managed to do, here is my blog post and also CodeProject article with demo, it will help all others. Thanks Richard
    • Marked as answer by Xyroid Tuesday, September 18, 2012 9:36 AM
    • Edited by Xyroid Wednesday, September 19, 2012 7:11 AM
    Tuesday, September 18, 2012 9:36 AM
  • You may want to remove your Bing Maps key from your blog post.

    http://rbrundritt.wordpress.com

    Tuesday, September 18, 2012 10:55 AM
  • Yes sure, thanks for noticing me
    Tuesday, September 18, 2012 12:36 PM