🌎

MapKitのルート検索APIを全部見る

2024/09/03に公開

APIを全部見るモチベーション

「iOSでルート検索」といったらまずは標準であるMapKitの MKDirections が思い浮かぶ。ざっくりこんな感じで使う:

let request = MKDirections.Request()
request.source = MKMapItem(placemark: source)
request.destination = MKMapItem(placemark: destination)

let directions = MKDirections(request: request)
let response = try await directions.calculate()

始点(source)と終点(destination)を決めてルート検索、基本的にこの使い方になる。経由地の指定もできない。

で、MapKit のルート検索(MKDirections)はできることが少ない、Google Directions APIだともっとできることが多そう、

・・・と思っていたのだが、Perplexityに壁打ち相手になってもらいながらGoogle Directions APIについて調べていくうちに、「いや、それMapKitでもできるぞ...」と思うことが多かった [1] ので、その整理のためにもいったん MapKitのルート検索APIでできることを全部洗い出してみる ことにした。

MKDirections

https://developer.apple.com/documentation/mapkit/mkdirections

iOS 7.0+, macOS 10.9+, visionOS 1.0+

A utility object that computes directions and travel-time information based on the route information you provide.
(あなたが入力したルート情報に基づいて、方向と所要時間情報を計算するユーティリティオブジェクト。)

Overview:

You use an MKDirections object to ask the Apple servers to provide walking or driving directions for a route, which you specify using an MKDirections.Request object. After making a request, MapKit delivers the results asynchronously to the completion handler that you provide. You can also get the estimated travel time for the route.
(MKDirectionsオブジェクトを使用して、Appleのサーバーに徒歩または車のルート案内を要求します。要求を行うと、MapKitは、あなたが用意した完了ハンドラに非同期で結果を返します。また、ルートの推定所要時間も取得できます。)

Each MKDirections object handles a single request for directions, although you can cancel and restart that request as needed. You can create multiple instances of this class and process different route requests at the same time, but make requests only when you plan to present the corresponding route information to the user. Apps may receive an MKError.Code.loadingThrottled error if the device makes too many requests in too short a time period.
(各 MKDirections オブジェクトは、1つのルート案内リクエストを処理しますが、必要に応じてそのリクエストをキャンセルしたり再開したりすることができます。このクラスの複数のインスタンスを作成し、同時に異なるルートリクエストを処理することもできますが、対応するルート情報をユーザに表示する予定がある場合のみリクエストを送信するようにしてください。デバイスが短時間で多数のリクエストを送信した場合、アプリケーションは MKError.Code.loadingThrottled エラーを受信することがあります。)

APIリファレンスを眺めた感じでは、本クラス自体には(ルート検索の)カスタマイズ性はなさそう。

MKDirections.Request

https://developer.apple.com/documentation/mapkit/mkdirections/request

The start and end points of a route, along with the planned mode of transportation.
(ルートの始点と終点、および予定されている交通手段。)

Overview:

You use an MKDirections.Request object when requesting or providing directions. If your app provides directions, use this class to decode the URL that the Maps app sends to you. If you need to request directions from Apple, pass an instance of this class to an MKDirections object. For example, an app that provides subway directions might request walking directions to and from relevant subway stations.
(MKDirections.Requestオブジェクトは、経路の要求または提供時に使用します。 アプリケーションが経路を提供する場合、このクラスを使用して、マップアプリケーションから受け取ったURLをデコードします。 Appleに経路を要求する必要がある場合は、このクラスのインスタンスをMKDirectionsオブジェクトに渡します。 例えば、地下鉄の経路を提供するアプリケーションは、関連する地下鉄の駅までの徒歩経路を要求する場合があります。)

Prior to iOS 14, for apps that provide directions, you receive direction-related URLs in your app delegate’s application(_:open:options:)method. Upon receiving a URL, call the isDirectionsRequest(_:) method of this class to determine whether the URL relates to routing directions. If it does, create an instance of this class using the provided URL and extract the map items associated with the start and end points.
(iOS 14より前のバージョンでは、経路案内を提供するアプリでは、アプリデリゲートのapplication(:open:options:)メソッドで経路案内関連のURLを受け取ります。URLを受け取ったら、このクラスのisDirectionsRequest(:)メソッドを呼び出して、URLが経路案内に関連するものかどうかを判断します。関連する場合は、提供されたURLを使用してこのクラスのインスタンスを作成し、開始地点と終了地点に関連するマップアイテムを抽出します。)

大前提となる始点と終点の指定をはじめ、ルート検索の細かいカスタマイズができるのはこのクラス。

移動手段の指定

まずは基本的なやつ。

var transportType: MKDirectionsTransportType

The type of conveyance that the directions apply to.

指定できる MKDirectionsTransportType の値は以下の4種類:

static var automobile: MKDirectionsTransportType

Directions suitable for use while driving.

static var walking: MKDirectionsTransportType

Directions suitable for a pedestrian.

static var transit: MKDirectionsTransportType

Directions suitable for public transportation.

static var any: MKDirectionsTransportType

Directions suitable for any transportation option.

デフォルトは any とのこと。

高速道路の利用

var highwayPreference: MKDirections.RoutePreference

The value that indicates whether the framework uses or avoids highways when providing directions.

any or avoid を指定できる。

有料道路の利用

var tollPreference: MKDirections.RoutePreference

The value that indicates whether the framework avoids routes that have tolls when providing directions.
(経路案内を行う際に有料道路を回避するかどうかを示す値。)

"tolls" は通行料、料金所という意味らしい。

any or avoid を指定できる。

複数ルートの探索

var requestsAlternateRoutes: Bool

A Boolean value that indicates whether your app requests multiple routes when they’re available.

When this property is false, the server returns a single route between the start and end points. When this property is true, the server may return additional routes for the user to follow. The server returns additional routes only if they’re available and represent a reasonable path that the user might take.
(このプロパティが false の場合、サーバーは開始ポイントと終了ポイント間の単一のルートを返します。このプロパティが true の場合、サーバーはユーザーがたどるための追加のルートを返すことがあります。サーバーが追加のルートを返すのは、それらが利用可能であり、ユーザーがたどる可能性のある妥当な経路である場合のみです。)

デフォルト値は false

出発日時と到着日時の指定

var departureDate: Date?

The departure date for the trip.

var arrivalDate: Date?

The arrival date for the trip.

Google Maps API との比較

書き出してみると、MapKitのルート検索APIでは(記事を書き始める時点で)思っていた以上にできることは少なかった...

Google Directions APIのドキュメントを見てみると、

https://developers.google.com/maps/documentation/directions/get-directions

概ね似たようなAPIではあるが、多少Google Directions APIの方ができることが多そうではある。

MapKitにはないがGoogle Directions APIにはある機能

  • region 指定
  • 移動手段の bicycling モード
  • traffic_model
    • best_guess / pessimistic / optimistic
  • transit_routing_preference
    • less_walking / fewer_transfers
  • units
  • waypoints
    • 中継地点の指定
    • MAX 25地点

Distance Matrix API

Google Directions APIとはまた別物になるが、Distance Matrix API というのもある。

https://developers.google.com/maps/documentation/distance-matrix/overview

出発地と目的地を含むHTTPSリクエストを受け取り、各組み合わせの移動距離と所要時間を返す、というもの。

ルート検索APIとは違い、ルート情報は返さない。

複数の出発地と目的地を指定できるので、最も効率的な移動ルートを決定するのに役立つ。

例えば、作業員をどの現場に派遣するか、またはどの倉庫から荷物を発送するか、といったユースケースを想定しているらしい。

脚注
  1. たとえばGoogle Directions APIでは経由地の指定ができるが、MapKitでも始点〜経由地、経由地〜終点と分割してルート検索をすれば同様のこと(もちろん完全に等価ではないが)が可能。 ↩︎

Discussion