地図へのルートとルート案内の表示

注意

MapControl とマップ サービスには、MapServiceToken と呼ばれるマップ認証キーが必要です。 マップ認証キーを取得して設定する方法について詳しくは、「マップ認証キーの要求」をご覧ください。

ルートとルート案内を要求し、アプリで表示します。

注意

アプリでの地図の使用について詳しくは、ユニバーサル Windows プラットフォーム (UWP) の地図サンプルをダウンロードしてください。 地図表示がアプリの主要機能でない場合は、代わりに Windows マップ アプリを起動することを検討します。 bingmaps:ms-drive-to:ms-walk-to: の各 UI スキームを使って、Windows マップ アプリを起動し、特定の地図やターン バイ ターン方式のルート案内を表示することができます。 詳しくは、「Windows マップ アプリの起動」をご覧ください。

 

MapRouteFinder 結果の概要

ルートとルート案内のクラスがどのように関連するかを次に示します。

  • MapRouteFinder クラスには、ルートとルート案内を取得するメソッドがあります。 これらのメソッドは、MapRouteFinderResult を返します。

  • MapRouteFinderResult には MapRoute オブジェクトが含まれています。 MapRouteFinderResultRoute プロパティを通じてこのオブジェクトにアクセスします。

  • MapRoute には、MapRouteLeg オブジェクトのコレクションが含まれています。 MapRouteLegs プロパティを通じてこのコレクションにアクセスします。

  • MapRouteLeg には、MapRouteManeuver オブジェクトのコレクションが含まれています。 MapRouteLegManeuvers プロパティを通じてこのコレクションにアクセスします。

自動車や徒歩でのルートとルート案内を取得するには、MapRouteFinder クラスのメソッドを呼び出します。 たとえば、GetDrivingRouteAsyncGetWalkingRouteAsync を使用できます。

ルートを要求する場合は、次の指定を行うことができます。

  • 始点と終点のみを指定するか、ルートを計算する一連の中間点を指定できます。

    立寄地を指定するとルート区間が追加され、各区間に独自の旅程が設定されます。 立寄地を指定するには、GetDrivingRouteFromWaypointsAsync のオーバーロードのいずれかを使います。

    経由地は、立寄地どうしの間の中間点を定義します。 ルート区間は追加されません。 これらは、通過する必要のあるルート上の中間点を示すだけです。 経由地を指定するには、GetDrivingRouteFromEnhancedWaypointsAsync のオーバーロードのいずれかを使います。

  • 最適化を指定できます (例: 距離を最短にする)。

  • 制限を指定できます (例: 高速道路を回避する)。

ルート案内の表示

MapRouteFinderResult オブジェクトには MapRoute オブジェクトが含まれており、Route プロパティを使ってアクセスできます。

計算された 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.

ルートの表示

MapControlMapRoute を表示するには、MapRoute を使って MapRouteView を構成します。 次に、MapControlRoutes コレクションに MapRouteView を追加します。

重要

マップ サービスまたはマップ コントロールを使用する前に、マップ認証キーを指定する必要があります。 詳しくは、「マップ認証キーの要求」をご覧ください。

 

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);
   }
}

この例では、MapWithRoute という名前の MapControl に次のルートが表示されます。

ルートが表示されたマップ コントロール。

同じ例を使って、2 つの立寄地の間に 1 つの経由地を指定するバージョンを次に示します。

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);
  }
}