Messin' with Bing Maps Directions Task in Windows Phone

DZone 's Guide to

Messin' with Bing Maps Directions Task in Windows Phone

· Mobile Zone ·
Free Resource

In this short tutorial we are going to see how to use a Bing Maps Direction Task available in Windows Phone Application development. With this task we can find a shortest driving direction and distance route between 2 specific points or location based on the user inputs.  Bing Maps Direction task uses Start and End properties which should be specified by the user to get the directions. At least one of the property is mandatory for this task to execute while the other point will be taken as default user location from which the application is accessing the Bing Maps. If both the Start and End properties are not specified the application will through an exception. Let us see the steps on how to achieve this task in real time for a Windows phone application.

Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Bing Maps Direction Task as shown in the screen below.


Now drag and drop few controls to the screen which is used to trigger event that launches the Bing Maps Directions Task. Once we designed the screen with the controls we can see the screen looks like below.


Now we need to write our code in the button click event to trigger the Bing Map Directions task on the user click. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.

using Microsoft.Phone.Tasks;
using System.Device.Location;

For System.Device.Location we need to manually add reference to System.Device Name space. Next is to add the below code to the button click event which basically creates an instance of the Bing Maps Directions task as shown in the code below.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Device.Location;

namespace F5debugHowto18
    public partial class MainPage : PhoneApplicationPage
        // Constructor
        public MainPage()

        private void button1_Click(object sender, RoutedEventArgs e)
            BingMapsDirectionsTask bmDirectionTask = new BingMapsDirectionsTask();
            LabeledMapLocation start = new LabeledMapLocation(txtFrom.Text.ToString(), null);
            LabeledMapLocation end = new LabeledMapLocation(txtTo.Text.ToString(), null);

            bmDirectionTask.Start = start;
            bmDirectionTask.End = end;





Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

Output Screens:




As you can see in the screens we can get the clear direction by Walk as well as by Car driving with the detailed explanation of the number of miles it will cover, no of hours to travel by car or by walk etc. Also we can see if the entered input string is not matching, then Bing Maps itself suggests the nearest matching words through which we can select the exact location which we are trying to get the directions. That’s it from this tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!




Published at DZone with permission of Karthikeyan Anbarasan , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}