How to use the Maps directions task for Windows Phone 8
August 19, 2014
Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1
The Maps directions task launches the Maps application and displays driving directions between two points. You can specify both the start and end points, or specify only one; in the latter case, the user’s current location is used for the other one. The start and end points contain a string label, and geographic coordinates specifying the latitude and longitude of the location. If you omit the geographic coordinates, the label string is used by the Maps application as a search term.
By using Launchers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers for Windows Phone 8.
The Maps application will not be launched if the device is in a location where it is not supported. Instead, a dialog is displayed to the user indicating that Maps is unavailable in their location.
To use the Maps directions task
Add a reference to the System.Device assembly to your project.
Add the following statements to your code.
Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.
MapsDirectionsTask mapsDirectionsTask = new MapsDirectionsTask(); // You can specify a label and a geocoordinate for the end point. // GeoCoordinate spaceNeedleLocation = new GeoCoordinate(47.6204,-122.3493); // LabeledMapLocation spaceNeedleLML = new LabeledMapLocation("Space Needle", spaceNeedleLocation); // If you set the geocoordinate parameter to null, the label parameter is used as a search term. LabeledMapLocation spaceNeedleLML = new LabeledMapLocation("Space Needle", null); mapsDirectionsTask.End = spaceNeedleLML; // If mapsDirectionsTask.Start is not set, the user's current location is used as the start point. mapsDirectionsTask.Show();
You must specify at least one of Start or End, or an exception is thrown when you call the Show method.