交通状況の確認

専用のTrafficEngineを使用すると、事故、建設工事、道路閉鎖などの現在の交通障害について、詳細かつローカライズ可能な情報を照会できます。

1 行のコードでマップ レイヤー 状態TRAFFIC_INCIDENTS を有効にすることで、マップ上の交通障害を視覚化できます。 HERE SDK は 、レイヤーTRAFFIC_FLOWを追加することで現在のトラフィック状況を確認するための別のレイヤーもサポートしています。

また、経路情報 セクションに示されているように、インスタンスRouteに沿ったトラフィックを指定することもできます。

リアルタイムの交通状況とインシデントをマップに表示

マップ レイヤーの状態TRAFFIC_INCIDENTSを有効にすると、マップ上の交通障害を簡単に視覚化できます。 HERE SDK は、現在のトラフィック状況を確認するための別のレイヤーもサポートしています。 マップ上のレイヤーを表示または非表示にする方法については、以下の例を参照してください。

スクリーンショット: マップ上に交通障害を表示。

レイヤーを設定すると、マップの表示領域が自動的に更新されます。 そのため、マップをあらゆる方向に自由に移動して、最新の交通障害を確認できます。

多くの状況で、ドライバーは、都市内外での現在の渋滞状況に基づいて最速ルートを探しています。 HERE SDK を使用すると、現在のすべての渋滞を保持しているレイヤーをさまざまな色の線で表示して、渋滞の重大度をリアルタイムで常に更新することができます。 この機能を使用するには、オンライン接続が必要です。また、より多くのデータを消費します。 ただし、交通渋滞ルートはマップのタイルの一部として表示されるため、高性能です。

これらの交通情報は、いくつかのコード行でマップ上に表示することも、単独で表示することもできます。

private void enableTrafficVisualization() {
    Map<String, String> mapFeatures = new HashMap<>();
    // Once these traffic layers are added to the map, they will be automatically updated while panning the map.
    mapFeatures.put(MapFeatures.TRAFFIC_FLOW, MapFeatureModes.TRAFFIC_FLOW_WITH_FREE_FLOW);
    // MapFeatures.TRAFFIC_INCIDENTS renders traffic icons and lines to indicate the location of incidents.
    mapFeatures.put(MapFeatures.TRAFFIC_INCIDENTS, MapFeatureModes.DEFAULT);
    mapView.getMapScene().enableFeatures(mapFeatures);
}

新しいレイヤー状態の設定は同期的に実行されますが、以前にロードしておく必要がある有効なマップ シーン が必要です。 また、新しいマップ シーン のロード中に新しい機能の状態を設定すると、例外が発生する可能性があります。 機能レイヤーを非表示にするには、次を呼び出します。

private void disableTrafficVisualization() {
    List<String> mapFeatures = new ArrayList<>();
    mapFeatures.add(MapFeatures.TRAFFIC_FLOW);
    mapFeatures.add(MapFeatures.TRAFFIC_INCIDENTS);
    mapView.getMapScene().disableFeatures(mapFeatures);
}

トラフィックフローラインは、次のように色分けされています。

  • 緑 : 通常 の交通状況
  • 黄色 / 黄色 : 渋滞しています
  • 赤 : 交通量が非常に多い
  • 黒 : トラフィックのブロック

スクリーンショット: マップ上にインシデントとともに表示される交通量。

マップから交通障害を選択

MapViewTRAFFIC_INCIDENTSが表示されている場合は、タップハンドラを設定し、交通障害を選択して詳細情報を取得できます。

private void setTapGestureHandler() {
    mapView.getGestures().setTapListener(touchPoint -> {
        GeoCoordinates touchGeoCoords = mapView.viewToGeoCoordinates(touchPoint);
        // Can be null when the map was tilted and the sky was tapped.
        if (touchGeoCoords != null) {
            // Pick incidents that are shown in TRAFFIC_INCIDENTS.
            pickTrafficIncident(touchPoint);
        }
    });
}

// Traffic incidents can only be picked, when TRAFFIC_INCIDENTS is visible.
private void pickTrafficIncident(Point2D touchPointInPixels) {
    Point2D originInPixels = new Point2D(touchPointInPixels.x, touchPointInPixels.y);
    Size2D sizeInPixels = new Size2D(1, 1);
    Rectangle2D rectangle = new Rectangle2D(originInPixels, sizeInPixels);

    mapView.pickMapContent(rectangle, new MapViewBase.PickMapContentCallback() {
        @Override
        public void onPickMapContent(@Nullable PickMapContentResult pickMapContentResult) {
            if (pickMapContentResult == null) {
                // An error occurred while performing the pick operation.
                return;
            }

            List<PickMapContentResult.TrafficIncidentResult> trafficIncidents =
                    pickMapContentResult.getTrafficIncidents();
            if (trafficIncidents.size() == 0) {
                Log.d(TAG, "No traffic incident found at picked location");
            } else {
                Log.d(TAG, "Picked at least one incident.");
                PickMapContentResult.TrafficIncidentResult firstIncident = trafficIncidents.get(0);
                showDialog("Traffic incident picked:", "Type: " +
                        firstIncident.getType().name());

                // Find more details by looking up the ID via TrafficEngine.
                findIncidentByID(firstIncident.getOriginalId());
            }

            // Optionally, look for more map content like embedded POIs.
        }
    });
}

タップハンドラを使用すると、タッチした位置がビューの座標で取得され、 mapView.pickMapContent()に渡すことができます。 ここでは、ポイントサイズの四角形を使用しますが、一度に複数のコンテンツを含めるようにより大きな領域に拡大することもできます。

コールバックには、 常にマップに表示される、TrafficIncidentResultが含まれているが、デフォルトの POI マーカーなどのその他の埋め込みタイプも含まれているPickMapContentResultがあります。 このタイプTrafficIncidentResult では、インシデントに関するほとんどの情報がすでに提供されていますが、利用可能なすべての情報を取得するには、 TrafficEngineを使用して(以下のも参照)、 ID で選択したインシデントを検索します。

private void findIncidentByID(String originalId) {
    TrafficIncidentLookupOptions trafficIncidentsQueryOptions = new TrafficIncidentLookupOptions();
    // Optionally, specify a language:
    // the language of the country where the incident occurs is used.
    // trafficIncidentsQueryOptions.languageCode = LanguageCode.EN_US;
    trafficEngine.lookupIncident(originalId, trafficIncidentsQueryOptions, new TrafficIncidentLookupCallback() {
        @Override
        public void onTrafficIncidentFetched(@Nullable TrafficQueryError trafficQueryError, @Nullable TrafficIncident trafficIncident) {
            if (trafficQueryError == null) {
                Log.d(TAG, "Fetched TrafficIncident from lookup request." +
                        " Description: " + trafficIncident.getDescription().text);
            } else {
                showDialog("TrafficLookupError:", trafficQueryError.toString());
            }
        }
    });
}

使用例は、トラフィック のアプリの一部として、 GitHub で入手可能。

TrafficEngine を使用してインシデントを照会

TrafficEngineでは、進行中の交通障害に関する詳細情報を取得できます。 GeoCorridorGeoBox、またはGeoCircle を介して、ルートに沿ったインシデントを検索できます。

また、複数のオプションを設定して、特定のタイプのインシデントのみを検索することもできます。 その結果、該当する車両タイプ、日付情報、位置情報、ローカライズ可能な説明、概要などの結果が得られます。

まず、次のもの TrafficEngineの新しいインスタンスを作成します。

try {
    trafficEngine = new TrafficEngine();
} catch (InstantiationErrorException e) {
    throw new RuntimeException("Initialization of TrafficEngine failed: " + e.error.name());
}

以下では、GeoCircle の内部で可能性のある、TrafficIncidentsを検索します 。

private void queryForIncidents(GeoCoordinates centerCoords) {
    int radiusInMeters = 1000;
    GeoCircle geoCircle = new GeoCircle(centerCoords, radiusInMeters);
    TrafficIncidentsQueryOptions trafficIncidentsQueryOptions = new TrafficIncidentsQueryOptions();
    // Optionally, specify a language:
    // the language of the country where the incident occurs is used.
    // trafficIncidentsQueryOptions.languageCode = LanguageCode.EN_US;
    trafficEngine.queryForIncidents(geoCircle, trafficIncidentsQueryOptions, new TrafficIncidentsQueryCallback() {
        @Override
        public void onTrafficIncidentsFetched(@Nullable TrafficQueryError trafficQueryError,
                                              @Nullable List<TrafficIncident> trafficIncidentsList) {
            if (trafficQueryError == null) {
                // If error is null, it is guaranteed that the list will not be null.
                String trafficMessage = "Found " + trafficIncidentsList.size() + " result(s). See log for details.";
                TrafficIncident nearestIncident =
                        getNearestTrafficIncident(centerCoords, trafficIncidentsList);
                if (nearestIncident != null) {
                    trafficMessage += " Nearest incident: " + nearestIncident.getDescription().text;
                }
                showDialog("Nearby traffic incidents", trafficMessage);

                for (TrafficIncident trafficIncident : trafficIncidentsList) {
                    Log.d(TAG, "" + trafficIncident.getDescription().text);
                }
            } else {
                showDialog("TrafficQueryError:", trafficQueryError.toString());
            }
        }
    });
}

見つかった各 TrafficIncident項目について、その説明を記録します。 ドイツで検索する場合、デフォルトではドイツ語で結果が表示されます。 次のように表示されます。

Berlin, Sachsendamm zwischen Gotenstraße und Priesterweg Fahrbahn auf einen Fahrstreifen verengt, Staugefahr, Bauarbeiten, bis voraussichtlich 20.02.2028

別の言語を指定することもできます。たとえば、次のように設定します。

trafficIncidentsQueryOptions.languageCode = LanguageCode.EN_US;

TRAFFIC_INCIDENTS は、障害の場所を示す交通アイコンと線をレンダリングするレイヤーであることに注意してください。。

TrafficIncident には、その場所を示すGeoPolylineが含まれています。 マップ上のタップした場所とポリライン に含まれている地理座標を比較することで、マップ上のタップした場所に最も近いインシデントを見つけることができます。

@Nullable
private TrafficIncident getNearestTrafficIncident(GeoCoordinates currentGeoCoords,
                                                  List<TrafficIncident> trafficIncidentsList) {
    if (trafficIncidentsList.size() == 0) {
        return null;
    }

    // By default, traffic incidents results are not sorted by distance.
    double nearestDistance = Double.MAX_VALUE;
    TrafficIncident nearestTrafficIncident = null;
    for (TrafficIncident trafficIncident : trafficIncidentsList) {
        // In case lengthInMeters == 0 then the polyline consistes of two equal coordinates.
        // It is guaranteed that each incident has a valid polyline.
        for (GeoCoordinates geoCoords : trafficIncident.getLocation().polyline.vertices) {
            double currentDistance = currentGeoCoords.distanceTo(geoCoords);
            if (currentDistance < nearestDistance) {
                nearestDistance = currentDistance;
                nearestTrafficIncident = trafficIncident;
            }
        }
    }

    return nearestTrafficIncident;
}

もちろん、MapPolylineを追加することで、自分自身でインシデントのポリラインをマップに表示することもできます。 MapMarker インシデントのタイプに応じて、別のイメージを選択するように独自のアイコンを追加することもできます。

ラジオ局から交通放送を受信

TrafficDataProvider インターフェイスを使用すると、 HERE SDK は交通放送を提供するラジオ局の信号を統合できます。

TrafficBroadcastRDS-TMC 形式を想定しており 、インターネットに接続されていない場合に使用できます。これにより、OfflineRoutingEngine はラジオチャネル経由で送信される交通データを利用できます。

トラフィックデータイベントを受信するには、メソッドtrafficBroadcast.activate() を呼び出す必要があります。

TrafficBroadcastは 、位置情報ソースから提供された新しい位置に継続的に反応し、LocationListenerとして機能します。 activate()のコールの種類にかかわら ず、位置情報を更新する必要があります。

インターフェイスを採用するには、特別なハードウェアが必要です。 詳細については、 HERE の担当者にお問い合わせください。 これはベータ版の機能としてリリースされています。

」に一致する結果は 件です

    」に一致する結果はありません