구글 지도 Routes API 웨이포인트 최적화: Optimize Waypoints

여러 개의 픽업·배송 지점을 방문해야 할 때, 사람이 일일이 최적 경로를 계획하기란 번거롭고 실수도 잦습니다. Google 지도 Routes API의 optimizeWaypointOrder 기능을 활용하면, 최대 23개의 중간 경유지(intermediateWaypoints)에 대해 방문 순서를 자동으로 최적화해 주어 효율적인 라우팅을 구현할 수 있습니다.

 

1. Optimize Waypoints란?

  • optimizeWaypointOrder: true를 설정하면, API가 내부적으로 TSP(Travelling Salesman Problem) 알고리즘을 적용해 최적의 방문 순서를 계산합니다.
  • 원래 입력한 순서와 상관없이, 전체 이동 거리 또는 소요 시간을 최소화하는 경로를 반환합니다.
  • 최대 23개의 중간 경유지에 대해 지원하며, 그 이상은 오류가 발생합니다.

2. 전제 조건

  • travelModeDRIVE, BICYCLE, TWO_WHEELER 중 하나여야 합니다.
  • 출발지(origin)와 도착지(destination)는 필수로 지정해야 합니다.
  • 중간 경유지(intermediateWaypoints)는 1개 이상 23개 이하로 설정해야 합니다.
  • computeAlternativeRoutesrouteModifiers 옵션과 병행 사용이 가능합니다.

3. 사용법: optimizeWaypointOrder 설정

Compute Routes 호출 본문에 다음과 같은 형식으로 optimizeWaypointOrder를 포함합니다.

{
  "origin": { "location": { "latLng": { "latitude": 37.5665, "longitude": 126.9780 } } },
  "intermediates": [
    { "location": { "latLng": { "latitude": 37.5700, "longitude": 126.9820 } } },
    { "location": { "latLng": { "latitude": 37.5750, "longitude": 126.9900 } } },
    { "location": { "latLng": { "latitude": 37.5800, "longitude": 126.9920 } } }
  ],
  "destination": { "location": { "latLng": { "latitude": 37.5512, "longitude": 126.9882 } } },
  "travelMode": "DRIVE",
  "optimizeWaypointOrder": true
}

4. curl 요청 예시

curl -X POST 'https://routes.googleapis.com/directions/v2:computeRoutes' \
  -H 'Content-Type: application/json' \
  -H 'X-Goog-Api-Key: YOUR_API_KEY' \
  -H 'X-Goog-FieldMask: routes.legs' \
  --data-raw '{
    "origin": {
      "location": { "latLng": { "latitude": 37.5665, "longitude": 126.9780 } }
    },
    "intermediates": [
      { "location": { "latLng": { "latitude": 37.5700, "longitude": 126.9820 } } },
      { "location": { "latLng": { "latitude": 37.5750, "longitude": 126.9900 } } },
      { "location": { "latLng": { "latitude": 37.5800, "longitude": 126.9920 } } }
    ],
    "destination": {
      "location": { "latLng": { "latitude": 37.5512, "longitude": 126.9882 } }
    },
    "travelMode": "DRIVE",
    "optimizeWaypointOrder": true
  }'

5. 응답 예시

{
  "routes": [
    {
      "legs": [
        {
          "startWaypointIndex": 0,
          "endWaypointIndex": 2,
          "distanceMeters": 800,
          "duration": "180s",
          "startLocation": { "latLng": { "latitude": 37.5665, "longitude": 126.9780 } },
          "endLocation":   { "latLng": { "latitude": 37.5800, "longitude": 126.9920 } }
        },
        {
          "startWaypointIndex": 2,
          "endWaypointIndex": 1,
          "distanceMeters": 1200,
          "duration": "240s",
          "startLocation": { "latLng": { "latitude": 37.5800, "longitude": 126.9920 } },
          "endLocation":   { "latLng": { "latitude": 37.5750, "longitude": 126.9900 } }
        },
        {
          "startWaypointIndex": 1,
          "endWaypointIndex": 3,
          "distanceMeters": 1000,
          "duration": "210s",
          "startLocation": { "latLng": { "latitude": 37.5750, "longitude": 126.9900 } },
          "endLocation":   { "latLng": { "latitude": 37.5512, "longitude": 126.9882 } }
        }
      ]
    }
  ]
}

6. 활용 팁

  • 배송·픽업 서비스: 여러 건의 배달을 최적화해 연료 비용과 시간을 절감할 수 있습니다.
  • 필드 서비스: 기술자 방문 스케줄을 자동으로 정렬해 이동 효율을 높입니다.
  • 응답 확인: startWaypointIndexendWaypointIndex를 통해 실제 방문 순서를 파악하세요.

7. 주의사항 및 요금

  • 최대 23개의 중간 경유지까지 지원하며, 초과 시 INVALID_ARGUMENT 오류가 발생합니다.
  • 복잡한 최적화 과정으로 인해 단일 경로 요청보다 처리 시간이 길어질 수 있습니다.
  • Optimize Waypoints 기능은 Pro 요금제에 포함되므로, 사용 시 비용을 확인하세요.

optimizeWaypointOrder 옵션을 활용하면 여러 중간 경유지의 최적 방문 순서를 손쉽게 계산할 수 있어, 라우팅 효율성과 사용자 만족도를 동시에 높일 수 있습니다.