지도에 경로 및 길 찾기 표시

참고 항목

MapControl 및 지도 서비스를 사용하려면 MapServiceToken이라는 지도 인증 키가 필요합니다. 맵 인증 키 가져오기 및 설정에 대한 자세한 내용은 맵 인증 키 요청을 참조하세요.

경로 및 길 찾기를 요청하고 앱에 표시합니다.

참고 항목

앱에서 지도 사용에 대한 자세한 내용을 알아보려면 UWP(유니버설 Windows 플랫폼) 지도 샘플을 다운로드하세요. 매핑이 앱의 핵심 기능이 아닌 경우 대신 Windows 지도 앱을 실행하는 것이 좋습니다. bingmaps:, ms-drive-to:ms-walk-to: URI 체계를 사용하면 Windows 지도 앱을 특정 지도 및 교대 방향으로 시작할 수 있습니다. 자세한 내용은 Windows 지도 앱 실행을 참조하세요.

 

MapRouteFinder 결과 소개

경로 및 방향에 대한 클래스를 관련시키는 방법은 다음과 같습니다.

MapRouteFinder 클래스의 메서드를 호출하여 운전 또는 보행 경로와 길 찾기를 가져옵니다. 예를 들어 GetDrivingRouteAsync 또는 GetWalkingRouteAsync입니다.

경로를 요청할 때 지정할 수 있는 항목은 다음과 같습니다.

  • 시작점과 끝점만 제공할 수도 있고, 경로를 계산하기 위해 일련의 웨이포인트를 제공할 수도 있습니다.

    정지 웨이포인트는 각각의 여정이 있는 추가 경로 다리를 추가합니다. 정지 웨이포인트를 지정하려면 GetDrivingRouteFromWaypointsAsync 오버로드 중에서 사용합니다.

    경유 웨이포인트는 정지 웨이포인트 사이의 중간 위치를 정의합니다. 이는 경로 다리를 추가하지 않습니다. 단지 경로가 통과해야 하는 웨이포인트일 뿐입니다. 정지 웨이포인트를 지정하려면 GetDrivingRouteFromEnhancedWaypointsAsync 오버로드 중에서 사용합니다.

  • 최적화(예제: 거리 최적화)를 지정할 수 있습니다.

  • 제한 사항(예제: 고속도로 회피)을 지정할 수 있습니다.

방향 표시

MapRouteFinderResult 개체에는 Route(경로) 속성을 통해 액세스할 수 있는 MapRoute 개체가 포함되어 있습니다.

계산된 MapRoute에는 경로, 경로의 길이 및 경로의 구간을 포함하는 MapRouteLeg 개체의 컬렉션을 트래버스할 시간을 제공하는 속성이 있습니다. 각 MapRouteLeg 개체에는 MapRouteManeuver 개체의 컬렉션이 포함되어 있습니다. MapRouteManeuver 개체에는 InstructionText 속성을 통해 액세스할 수 있는 방향이 포함되어 있습니다.

Important

지도 서비스를 사용하려면 먼저 지도 인증 키를 지정해야 합니다. 자세한 내용은 지도 인증 키 요청을 참조하세요.

 

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를 추가하세요.

Important

지도 서비스 또는 지도 컨트롤을 사용하려면 먼저 지도 인증 키를 지정해야 합니다. 자세한 내용은 지도 인증 키 요청을 참조하세요.

 

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에 다음 항목을 표시합니다.

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