如何为 Windows Phone 8 请求行车或步行路线
2013/3/11
适用于:仅限于 Windows Phone 8。
本主题描述如何编写一个应用,用于从安装在手机上的另一应用请求到指定目的地的行车或步行路线。您通过使用 ms-drive-to 或 ms-walk-to Uri 方案来请求路线。
有关使用 URI 来启动另一应用的更多信息,请参见使用 Windows Phone 8 文件和 URI 关联的自动启动应用。有关如何编写响应方向请求的应用的信息,请参见如何响应 Windows Phone 8 的方向请求。
下面的屏幕快照演示允许用户请求到所选目的地的行车路线的内置应用。

本主题包含以下各节。
下面的代码示例演示如何从安装在手机上的另一应用请求到指定目的地的行车路线。此示例获取所需的参数值后,会将其汇编到 Uri 中。然后将使用 LaunchUriAsync 方法来调用 URI。
请求路线的步骤
在 Visual Studio 中,创建一个空的新 Windows Phone 项目。
在主页的代码隐藏文件中,粘贴以下方法。
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. } }在类的构造函数中,添加对 RequestDirections 方法的调用。
运行应用。
如果模拟器或手机没有任何可提供路线的应用,则会提示您在 Windows Phone 商店 中查找应用。
如果模拟器或手机仅有一个提供路线的应用,则会自动启动该应用。
如果模拟器或手机有多个提供路线的应用,则会提示您从列表中选择一个应用。
若要测试此方法,