在地圖上顯示路線和方向

注意

MapControl 和地圖服務要求地圖驗證金鑰,稱為 MapServiceToken。 如需取得和設定地圖驗證金鑰的詳細資訊,請參閱要求地圖驗證金鑰

要求路線和路線指引,並在應用程式中加以顯示。

注意

若要深入瞭解如何在應用程式中使用地圖,請下載通用 Windows 平台 (UWP) 地圖範例。 如果地圖不是應用程式的核心功能,請考慮改為啟動 Windows 地圖應用程式。 您可以使用 bingmaps:ms-drive-to:ms-walk-to: URI 配置,將 Windows 地圖應用程式啟動至特定地圖和導航路線。 如需詳細資訊,請參閱啟動 Windows 地圖應用程式

 

MapRouteFinder 結果簡介

以下是路線和方向的類別之間的關係:

呼叫 MapRouteFinder 類別的方法,以取得駕駛或步行路線和方向指示。 例如,GetDrivingRouteAsyncGetWalkingRouteAsync

當您要求路線時,您可以指定下列事項:

  • 您可以只提供起點和終點,也可以提供一系列的航點來計算路線。

    停止航點增加了額外的路線區段,每個路線都有自己的行程。 若要指定停止航點,請使用任何 GetDrivingRouteFromWaypointsAsync 多載。

    通過航點定義停止航點之間的中繼位置。 它們不會增加路線區段。 它們僅僅是路線必須通過的航點。 若要指定通過航點,請使用任何 GetDrivingRouteFromEnhancedWaypointsAsync 多載。

  • 您可以指定最佳化 (例如:最短距離)。

  • 您可以指定限制 (例如:避開高速公路)。

顯示方向

MapRouteFinderResult 物件包含您可以透過其 Route 屬性存取的 MapRoute 物件。

計算的 MapRoute 具有屬性,可提供周遊路線的時間、路線長度,以及包含路線區段的 MapRouteLeg 物件的集合。 每個 MapRouteLeg 物件都包含 MapRouteManeuver 物件的集合。 MapRouteManeuver 物件包含您可以透過其 InstructionText 屬性存取的指示。

重要

您必須先指定地圖驗證金鑰,才能使用地圖服務。 如需詳細資訊,請參閱要求地圖驗證金鑰

 

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Services.Maps;
using Windows.Devices.Geolocation;
...
private async void button_Click(object sender, RoutedEventArgs e)
{
   // Start at Microsoft in Redmond, Washington.
   BasicGeoposition startLocation = new BasicGeoposition() {Latitude=47.643,Longitude=-122.131};

   // End at the city of Seattle, Washington.
   BasicGeoposition endLocation = new BasicGeoposition() {Latitude = 47.604,Longitude= -122.329};

   // Get the route between the points.
   MapRouteFinderResult routeResult =
         await MapRouteFinder.GetDrivingRouteAsync(
         new Geopoint(startLocation),
         new Geopoint(endLocation),
         MapRouteOptimization.Time,
         MapRouteRestrictions.None);

   if (routeResult.Status == MapRouteFinderStatus.Success)
   {
      System.Text.StringBuilder routeInfo = new System.Text.StringBuilder();

      // Display summary info about the route.
      routeInfo.Append("Total estimated time (minutes) = ");
      routeInfo.Append(routeResult.Route.EstimatedDuration.TotalMinutes.ToString());
      routeInfo.Append("\nTotal length (kilometers) = ");
      routeInfo.Append((routeResult.Route.LengthInMeters / 1000).ToString());

      // Display the directions.
      routeInfo.Append("\n\nDIRECTIONS\n");

      foreach (MapRouteLeg leg in routeResult.Route.Legs)
      {
         foreach (MapRouteManeuver maneuver in leg.Maneuvers)
         {
            routeInfo.AppendLine(maneuver.InstructionText);
         }
      }

      // Load the text box.
      tbOutputText.Text = routeInfo.ToString();
   }
   else
   {
      tbOutputText.Text =
            "A problem occurred: " + routeResult.Status.ToString();
   }
}

此範例會在 tbOutputText 文字文字方塊中顯示下列結果。

Total estimated time (minutes) = 18.4833333333333
Total length (kilometers) = 21.847

DIRECTIONS
Head north on 157th Ave NE.
Turn left onto 159th Ave NE.
Turn left onto NE 40th St.
Turn left onto WA-520 W.
Enter the freeway WA-520 from the right.
Keep left onto I-5 S/Portland.
Keep right and leave the freeway at exit 165A towards James St..
Turn right onto James St.
You have reached your destination.

顯示路線

若要在 MapControl 上顯示 MapRoute,請使用 MapRoute 建構 MapRouteView。 然後,將 MapRouteView 新增至 MapControlRoutes 集合。

重要

您必須先指定地圖驗證金鑰,才能使用地圖服務或地圖控制項。 如需詳細資訊,請參閱要求地圖驗證金鑰

 

using System;
using Windows.Devices.Geolocation;
using Windows.Services.Maps;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Maps;
...
private async void ShowRouteOnMap()
{
   // Start at Microsoft in Redmond, Washington.
   BasicGeoposition startLocation = new BasicGeoposition() { Latitude = 47.643, Longitude = -122.131 };

   // End at the city of Seattle, Washington.
   BasicGeoposition endLocation = new BasicGeoposition() { Latitude = 47.604, Longitude = -122.329 };


   // Get the route between the points.
   MapRouteFinderResult routeResult =
         await MapRouteFinder.GetDrivingRouteAsync(
         new Geopoint(startLocation),
         new Geopoint(endLocation),
         MapRouteOptimization.Time,
         MapRouteRestrictions.None);

   if (routeResult.Status == MapRouteFinderStatus.Success)
   {
      // Use the route to initialize a MapRouteView.
      MapRouteView viewOfRoute = new MapRouteView(routeResult.Route);
      viewOfRoute.RouteColor = Colors.Yellow;
      viewOfRoute.OutlineColor = Colors.Black;

      // Add the new MapRouteView to the Routes collection
      // of the MapControl.
      MapWithRoute.Routes.Add(viewOfRoute);

      // Fit the MapControl to the route.
      await MapWithRoute.TrySetViewBoundsAsync(
            routeResult.Route.BoundingBox,
            null,
            Windows.UI.Xaml.Controls.Maps.MapAnimationKind.None);
   }
}

本範例會在名為 MapWithRouteMapControl 上顯示下列內容。

map control with route displayed.

以下是此範例的版本,在兩個停止航點之間使用通過航點:

using System;
using Windows.Devices.Geolocation;
using Windows.Services.Maps;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Maps;
...
private async void ShowRouteOnMap()
{
  Geolocator locator = new Geolocator();
  locator.DesiredAccuracyInMeters = 1;
  locator.PositionChanged += Locator_PositionChanged;

  BasicGeoposition point1 = new BasicGeoposition() { Latitude = 47.649693, Longitude = -122.144908 };
  BasicGeoposition point2 = new BasicGeoposition() { Latitude = 47.6205, Longitude = -122.3493 };
  BasicGeoposition point3 = new BasicGeoposition() { Latitude = 48.649693, Longitude = -122.144908 };

  // Get Driving Route from point A  to point B thru point C
  var path = new List<EnhancedWaypoint>();

  path.Add(new EnhancedWaypoint(new Geopoint(point1), WaypointKind.Stop));
  path.Add(new EnhancedWaypoint(new Geopoint(point2), WaypointKind.Via));
  path.Add(new EnhancedWaypoint(new Geopoint(point3), WaypointKind.Stop));

  MapRouteFinderResult routeResult =  await MapRouteFinder.GetDrivingRouteFromEnhancedWaypointsAsync(path);

  if (routeResult.Status == MapRouteFinderStatus.Success)
  {
      MapRouteView viewOfRoute = new MapRouteView(routeResult.Route);
      viewOfRoute.RouteColor = Colors.Yellow;
      viewOfRoute.OutlineColor = Colors.Black;

      myMap.Routes.Add(viewOfRoute);

      await myMap.TrySetViewBoundsAsync(
            routeResult.Route.BoundingBox,
            null,
            Windows.UI.Xaml.Controls.Maps.MapAnimationKind.None);
  }
}