此主题尚未评级 - 评价此主题

如何为 Windows Phone 8 请求行车或步行路线

2013/3/11

适用于:仅限于 Windows Phone 8。

本主题描述如何编写一个应用,用于从安装在手机上的另一应用请求到指定目的地的行车或步行路线。您通过使用 ms-drive-toms-walk-to Uri 方案来请求路线。

有关使用 URI 来启动另一应用的更多信息,请参见使用 Windows Phone 8 文件和 URI 关联的自动启动应用。有关如何编写响应方向请求的应用的信息,请参见如何响应 Windows Phone 8 的方向请求

下面的屏幕快照演示允许用户请求到所选目的地的行车路线的内置应用。

An app that uses the ms-drive-to protocol

本主题包含以下各节。

格式

用于启动路线请求的 URI 具有以下格式:

ms-drive-to:?destination.latitude=<latitude>&destination.longitude=<longitude>&destination.name=<name>

ms-walk-to:?destination.latitude=<latitude>&destination.longitude<longitude>&destination.name=<name>

您不在请求中指定起点。它将假设为当前位置。

您无需对 Uri 功 name 值进行编码。

示例

下面的示例演示到美国华盛顿州雷德蒙德市的路线请求的 URI。

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

下面的代码示例演示如何从安装在手机上的另一应用请求到指定目的地的行车路线。此示例获取所需的参数值后,会将其汇编到 Uri 中。然后将使用 LaunchUriAsync 方法来调用 URI。

请求路线的步骤

  1. 在 Visual Studio 中,创建一个空的新 Windows Phone 项目。

  2. 在主页的代码隐藏文件中,粘贴以下方法。

            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.
                }
            }
    
    
  3. 在类的构造函数中,添加对 RequestDirections 方法的调用。

  4. 运行应用。

    • 如果模拟器或手机没有任何可提供路线的应用,则会提示您在 Windows Phone 商店 中查找应用。

    • 如果模拟器或手机仅有一个提供路线的应用,则会自动启动该应用。

    • 如果模拟器或手机有多个提供路线的应用,则会提示您从列表中选择一个应用。

若要测试此方法,

本文是否对您有所帮助?
(1500 个剩余字符)
© 2013 Microsoft. 版权所有。