トラフィックの検索

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

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

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

リアルタイムのトラフィックフローとインシデントをマップに表示

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

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

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

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

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

mapView.mapScene.enableFeatures([MapFeatures.trafficFlow : MapFeatureModes.trafficFlowWithFreeFlow])
mapView.mapScene.enableFeatures([MapFeatures.trafficIncidents : MapFeatureModes.defaultMode])

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

mapView.mapScene.disableFeatures([MapFeatures.trafficFlow, MapFeatures.trafficIncidents])

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

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

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

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

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

// Conforming to TapDelegate protocol.
func onTap(origin: Point2D) {
    // Can be nil when the map was tilted and the sky was tapped.
    if let touchGeoCoords = mapView.viewToGeoCoordinates(viewCoordinates: origin) {
        tappedGeoCoordinates = touchGeoCoords

        // Pick incidents that are shown in trafficIncidents.
        pickTrafficIncident(touchPointInPixels: origin)
    }
}

// Traffic incidents can only be picked, when trafficIncidents is visible.
func pickTrafficIncident(touchPointInPixels: Point2D) {
    let originInPixels = Point2D(x: touchPointInPixels.x, y: touchPointInPixels.y)
    let sizeInPixels = Size2D(width: 1, height: 1)
    let rectangle = Rectangle2D(origin: originInPixels, size: sizeInPixels)

    mapView.pickMapContent(inside: rectangle, completion: onPickMapContent)
}

// MapViewBase.PickMapContentHandler to receive picked map content.
func onPickMapContent(mapContentResult: PickMapContentResult?) {
    if mapContentResult == nil {
        // An error occurred while performing the pick operation.
        return
    }

    let trafficIncidents = mapContentResult!.trafficIncidents
    if trafficIncidents.count == 0 {
        print("No traffic incident found at picked location")
    } else {
        print("Picked at least one incident.")
        let firstIncident = trafficIncidents.first!
        showDialog(title: "Traffic incident picked:", message: "Type: \(firstIncident.type.rawValue)")

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

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

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

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

func findIncidentByID(_ originalId: String) {
    let trafficIncidentsLookupOptions = TrafficIncidentLookupOptions()
    // Optionally, specify a language:
    // the language of the country where the incident occurs is used.
    // trafficIncidentsLookupOptions.languageCode = LanguageCode.EN_US
    trafficEngine.lookupIncident(with: originalId,
                                 lookupOptions: trafficIncidentsLookupOptions,
                                 completion: onTrafficIncidentCompletion)
}

// TrafficIncidentCompletionHandler to receive traffic incidents from ID.
func onTrafficIncidentCompletion(trafficQueryError: TrafficQueryError?, trafficIncident: TrafficIncident?) {
    if trafficQueryError == nil {
        print("Fetched TrafficIncident from lookup request." +
                " Description: " + trafficIncident!.description.text)
    } else {
        showDialog(title: "TrafficLookupError:", message: trafficQueryError.debugDescription)
    }
}

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

TrafficEngine を使用してインシデントのクエリー

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

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

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

do {
    try trafficEngine = TrafficEngine()
} catch let engineInstantiationError {
    fatalError("Failed to initialize TrafficEngine. Cause: \(engineInstantiationError)")
}

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

private func queryForIncidents(centerCoords: GeoCoordinates) {
    let geoCircle = GeoCircle(center: centerCoords, radiusInMeters: 1000)
    let trafficIncidentsQueryOptions = TrafficIncidentsQueryOptions()
    // Optionally, specify a language:
    // If the language is not supported, then the default behavior is applied and
    // the language of the country where the incident occurs is used.
    // trafficIncidentsQueryOptions.languageCode = LanguageCode.enUs
    trafficEngine.queryForIncidents(inside: geoCircle,
                                    queryOptions: trafficIncidentsQueryOptions,
                                    completion: onTrafficIncidentsFound);
}

// TrafficIncidentQueryCompletionHandler to receive traffic items.
func onTrafficIncidentsFound(error: TrafficQueryError?,
                             trafficIncidentsList: [TrafficIncident]?) {
    if let trafficQueryError = error {
        print("TrafficQueryError: \(trafficQueryError)")
        return
    }

    // If error is nil, it is guaranteed that the list will not be nil.
    var trafficMessage = "Found \(trafficIncidentsList!.count) result(s). See log for details."
    let nearestIncident = getNearestTrafficIncident(currentGeoCoords: tappedGeoCoordinates,
                                                    trafficIncidentsList: trafficIncidentsList!)
    trafficMessage.append(contentsOf: " Nearest incident: \(nearestIncident?.description.text ?? "nil")")
    showDialog(title: "Nearby traffic incidents",
               message: trafficMessage)

    for trafficIncident in trafficIncidentsList! {
        print(trafficIncident.description.text)
    }
}

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

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

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

trafficIncidentsQueryOptions.languageCode = LanguageCode.enUs

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

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

private func getNearestTrafficIncident(currentGeoCoords: GeoCoordinates,
                                       trafficIncidentsList: [TrafficIncident]) -> TrafficIncident? {
    if trafficIncidentsList.count == 0 {
        return nil
    }

    // By default, traffic incidents results are not sorted by distance.
    var nearestDistance: Double = Double.infinity
    var nearestTrafficIncident: TrafficIncident!
    for trafficIncident in trafficIncidentsList {
        // In case lengthInMeters == 0 then the polyline consistes of two equal coordinates.
        // It is guaranteed that each incident has a valid polyline.
        for geoCoords in trafficIncident.location.polyline.vertices {
            let currentDistance = currentGeoCoords.distance(to: geoCoords)
            if currentDistance < nearestDistance {
                nearestDistance = currentDistance
                nearestTrafficIncident = trafficIncident
            }
        }
    }

    return nearestTrafficIncident
}

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

ラジオ局から交通ブロードキャストを取得します

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

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

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

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

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

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

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