トラフィックを検索します

専用のを使用 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);
}

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

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

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

地図から交通事故を選択します

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

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()すると、タッチした位置がビューの座標で取得され、そこに渡すことができます。 HERE では、ポイントサイズの四角形を使用しますが、一度に複数のコンテンツを含めるようにピック領域を拡大することもできます。

コールバック には、 常に地図に表示される、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は、進行中の交通事故に関する詳細情報を取得できます。 経由、 GeoCorridor経由、または GeoBox 経由でルートに沿ってインシデントを検索 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 インシデントのタイプに応じて、別のイメージを選択するように独自のアイコンを追加することもできます。

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

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