How to request driving or walking directions from another app (XAML)
This topic describes how to write a Windows Phone app that requests driving or walking directions to a specified destination from another app installed on the phone. Your app requests directions by calling the ms-drive-to or ms-walk-to Uri scheme.
Tip This topic describes how to write code that requests directions from another app installed on the phone. The user may have more than one installed app that can handle requests for directions. When you use the method described in this topic, the user can choose which app to use to display the directions. If you simply want to display directions, you can also use the bingmaps: Uri scheme. For more info, see How to display maps and directions in the built-in Maps app.
Typically you call the LaunchUriAsync method to launch another app by using a Uri scheme such as ms-drive-to or ms-walk-to. For more info about calling a Uri to launch another app, see How to launch the default app for a URI.
For info about how to write an app that responds to requests for directions, see How to respond to requests for directions.
Important The methods described in this topic take the user to another app. The user has to go back to your app manually after getting directions from the other app.
The following screen shot shows a built-in app that lets the user request driving directions to the selected destination.
Format of the Uri
Format
The Uri to launch the request for directions has the following format:
ms-drive-to:?destination.latitude=<
latitude>&destination.longitude=<
longitude>&destination.name=<
name>
ms-walk-to:?destination.latitude=<
latitude>&destination.longitude=<
longitude>&destination.name=<
name>
You don’t specify the starting point in the request. The starting point is assumed to be the current location.
You do not have to encode the Uri or the name value.
Example
The following example shows the Uri to request directions to the city of Redmond, Washington, in the United States:
ms-drive-to:?destination.latitude=47.6451413797194&destination.longitude=-122.141964733601&destination.name=Redmond, WA
ms-walk-to:?destination.latitude=47.6451413797194&destination.longitude=-122.141964733601&destination.name=Redmond, WA
Requesting directions
The following code sample shows how to request driving directions to a specified destination from another app installed on the device. After this sample gets the required parameter values, it assembles them into a Uri. Then it calls the Uri with the LaunchUriAsync method.
To request directions
In Visual Studio, create an empty new UAP project.
In the code-behind file for the main page, paste the following method.
async void RequestDirections() { // Get the values required to specify the destination. string latitude = "47.6451413797194"; string longitude = "-122.141964733601"; string name = "Redmond, WA"; // Assemble the Uri to launch. Uri uri = new Uri("ms-drive-to:?destination.latitude=" + latitude + "&destination.longitude=" + longitude + "&destination.name=" + name); // The resulting Uri is: "ms-drive-to:?destination.latitude=47.6451413797194 // &destination.longitude=-122.141964733601&destination.name=Redmond, WA") // Launch the Uri. var success = await Windows.System.Launcher.LaunchUriAsync(uri); if (success) { // Uri launched. } else { // Uri failed to launch. } }
In the constructor of the class, add a call to the RequestDirections method.
Run the app.
- If the emulator or device has several apps that provide directions, you see a prompt to select one of the apps from a list.
- If the emulator or device has only one app that provides directions, that app starts automatically.
- If the emulator or device does not have any apps that provide directions, you see a prompt to find an app in the Windows Store.
For info about how to write an app that responds to requests for directions, see How to respond to requests for directions.