交通情報を更新する
最新の交通状況に基づいて、ルートを最新の状態に保ちます。このセクションでは、既存のルートの交通データを更新する方法と、交通状況が変化した際により適した代替ルートを見つけるために、ナビゲーション中に動的ルーティングを活用する方法について説明します。
ルート上の交通状況を更新する
ルートの交通情報は、calculateTrafficOnRoute()またはrefreshRoute()を呼び出すことで更新できます。
refreshRoute()メソッドは、出発地またはルートメタデータの更新が必要な場合に使用できます。- 交通情報のみを更新する必要があるシナリオでは、
calculateTrafficOnRoute()メソッドを使用してTrafficOnRouteオブジェクトを提供できます。現在の交通流と交通障害に関する詳細情報がTrafficOnSectionごとに記載されています。TrafficOnSectionには、TrafficOnSpanオブジェクトのリストとして、スパンごとの詳細な交通情報が記載されています。
どちらのメソッドでも、ルート距離とジオメトリーは変更されません。
注 (Navigateのみ)
または、
DynamicRoutingEngineを使用してナビゲーション中によりよいルートを検索することもできます。これらのルートは現在の交通流に基づいて最適化されます。その結果、交通量の多い道路を迂回するための新しいルートジオメトリーも含まれる場合があります。DynamicRoutingEngineの詳細については、「ナビゲーション」セクションを参照してください。
TrafficOnRouteを使用してルートプレビュー中に交通状況を更新する
routingEngine.calculateTrafficOnRoute(..)メソッドは、作成後に特定のセクションまたはルート全体の交通状況を更新します。ナビゲーション中またはルートプレビューコンテキストでこれを計算できます。
インデックスと最後にアクセスした位置からのオフセット (メートル単位) を使用して、最後に移動したルートセクションからの交通状況を再計算することで、これを行います。ルートプレビューコンテキストでは、ルート全体の交通情報を更新するために、lastTraveledSectionIndex traveledDistanceOnLastSectionInMetersを0に設定できます。
このメソッドは、主にルートプレビュー用に設計された画面で次のように利用できます。
public void updateTrafficOnRoute(Route route) {
// Since traffic is being calculated for the entire route, lastTraveledSectionIndex and traveledDistanceOnLastSectionInMeters are set to 0.
int lastTraveledSectionIndex = 0;
int traveledDistanceOnLastSectionInMeters = 0;
routingEngine.calculateTrafficOnRoute(route, lastTraveledSectionIndex, traveledDistanceOnLastSectionInMeters, new CalculateTrafficOnRouteCallback() {
@Override
public void onTrafficOnRouteCalculated(@Nullable RoutingError routingError, @Nullable TrafficOnRoute trafficOnRoute) {
if (routingError != null) {
Log.d(TAG, "CalculateTrafficOnRoute error: " + routingError.name());
} else {
showUpdatedETA(trafficOnRoute);
}
}
});
}TrafficOnRouteからの最新のETAはTrafficOnSpanから確認できます。各TrafficOnSpanはルート沿いの交通情報を提供します。次のメソッドを使用して更新されたETAを抽出できます。
private void showUpdatedETA(TrafficOnRoute trafficOnRoute) {
for (TrafficOnSection section : trafficOnRoute.trafficSections) {
List<TrafficOnSpan> spans = section.trafficSpans;
long updatedETAInSeconds = spans.stream()
.mapToLong(span -> span.duration.getSeconds())
.sum();
long updatedTrafficDelayInSeconds = spans.stream()
.mapToLong(span -> span.trafficDelay.getSeconds())
.sum();
String updatedETAString = String.format("Updated ETA %s\nUpdated traffic delay %s",
timeUtils.formatTime(updatedETAInSeconds),
timeUtils.formatTime(updatedTrafficDelayInSeconds));
showDialog("Updated traffic", updatedETAString);
}
}
注交通状況の更新では、予測モデルを使用してルートに沿った状況を予測しますが、マップレイヤーに表示されるリアルタイムの交通状況とは異なる場合があります。これらの違いの詳細については、「リアルタイムおよび予測交通データソースの違い」を参照してください。
ターン・バイ・ターンナビの実行中に交通情報を更新する (HERE SDK for Navigateのみ)
ターン・バイ・ターンナビの実行中は、calculateTrafficOnRoute()を呼び出して専用のTrafficOnRouteオブジェクトを計算することをお勧めします。交通状況は走行中に頻繁に変化する可能性があるため、アプリケーションはこの呼び出しを定期的に繰り返します。
以下の実装例では、設定可能な間隔で交通状況の更新が実行されます。
calculateTrafficOnRoute()が完了すると、VisualNavigatorを新しいTrafficOnRouteオブジェクトで更新することができます。これによりRouteProgressオブジェクトが提供する所要時間に関する情報が調整されます (HERE SDK for Navigateのみ)。
public void updateTrafficOnRoute(RouteProgress routeProgress, VisualNavigator visualNavigator) {
Route currentRoute = visualNavigator.getRoute();
if (currentRoute == null) {
// Should never happen.
return;
}
// Below, we use 10 minutes. A common range is between 5 and 15 minutes.
long trafficUpdateIntervalInMilliseconds = 10 * 60000; // 10 minutes.
long now = System.currentTimeMillis();
if ((now - lastTrafficUpdateInMilliseconds) < trafficUpdateIntervalInMilliseconds) {
return;
}
// Store the current time when we update trafficOnRoute.
lastTrafficUpdateInMilliseconds = now;
List<SectionProgress> sectionProgressList = routeProgress.sectionProgress;
SectionProgress lastSectionProgress = sectionProgressList.get(sectionProgressList.size() - 1);
int traveledDistanceOnLastSectionInMeters = currentRoute.getLengthInMeters() - lastSectionProgress.remainingDistanceInMeters;
int lastTraveledSectionIndex = routeProgress.routeMatchedLocation.sectionIndex;
routingEngine.calculateTrafficOnRoute(currentRoute, lastTraveledSectionIndex, traveledDistanceOnLastSectionInMeters, new CalculateTrafficOnRouteCallback() {
@Override
public void onTrafficOnRouteCalculated(@Nullable RoutingError routingError, @Nullable TrafficOnRoute trafficOnRoute) {
if (routingError != null) {
Log.d(TAG, "CalculateTrafficOnRoute error: " + routingError.name());
return;
}
// Sets traffic data for the current route, affecting RouteProgress duration in SectionProgress,
// while preserving route distance and geometry.
visualNavigator.setTrafficOnRoute(trafficOnRoute);
Log.d(TAG, "Updated traffic on route.");
}
});
}
注このコードはHERE Routingバックエンドへの定期的な呼び出しを開始します。契約に応じて、 通話ごとに個別に課金される場合があります。このコードの実行方法および実行頻度は、 アプリケーション側が決定します。
visualNavigator.setTrafficOnRoute()メソッドはすぐには有効になりません。その代わりに、更新された所要時間 (ETA) は次のRouteProgressイベントに反映されます。ETAをRouteProgressイベントから抽出する方法の詳細については、ETAのセクションを参照してください。
7 日前の更新










