Using directions with Bing Maps directions task

We got acquainted with Launchers in our previous topic. Let us now look at BingMapsDirectionsTask, and use it to provide driving directions from within our application:

  1. Create a copy of our HelloMaps-Pushpin project and rename it HelloMaps-Directions.
  2. Open your MainPage.xaml.cs file and import the Microsoft.Phone.Tasks namespace into your project.
    using Microsoft.Phone.Tasks;
    
  3. Assuming the maps center as Redmond, we want to show driving directions to the nearby Mercer Island as depicted in the following screenshot:
    Using directions with Bing Maps directions task
  4. We begin by defining a variable myDrivingDirection of type BingMapsDirectionsTask as:
    BingMapsDirectionsTask myDrivingDirection;
    
  5. In our MainPage() constructor, we define the start and end points of our route. The start being some location near Redmond and end being Mercer Island.
    // Show driving directions to Mercer Island
    // Use "null" as the starting location to use your current
    // location as the starting point.
    myDrivingDirection = new BingMapsDirectionsTask();
    myDrivingDirection.Start = new LabeledMapLocation
    ("My Location", newGeoCoordinate(47.6601, -122.13333));
    myDrivingDirection.End = new LabeledMapLocation
    ("Mercer Island", null);
    // End of driving directions
    
  6. We added a HyperlinkButton near the footer (as shown in the previous screenshot), that will be used to fire the start() method of the BingMapsDirectionsTask instance.
    private void hyperlinkButton1_Click(object sender,
    RoutedEventArgs e)
    {
    myDrivingDirection.Show();
    }
    
  7. Run the application now and click on the Show Directions hyperlink button. You should see the following output (tested on our Nokia Lumia 800):
    Using directions with Bing Maps directions task

Pretty simple, huh? It is that easy to incorporate directions in your apps no more extensive SDKs, libraries, and tons of code to import within your app. In pure Windows folklore we would say "Plug and Play" directions. You can find this example project in the code files for the book under Chapter 3, titled HelloMaps-Directions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.143.65