要求折線上的路況資訊

Routes Preferred API 可讓您要求折線沿途的路況資訊。路況會以速度類別 (NORMAL、SLOW、TRAFFIC_JAM) 表示,適用於回應折線的指定間隔。間隔是由折線起點 (含) 和終點 (不含) 的索引定義。

要求範例

車流量監測折線適用於路線層級和路段層級。在路線層級,車流速度資訊會以 SpeedReadingIntervals 的形式,提供在 RouteTravelAdvisory 回應欄位下方。如要接收路況資訊和路線折線,請在回應欄位遮罩中同時加入 polyline 和 speedReadingIntervals。

如果欄位遮罩包含 routes.legs.travelAdvisory.speedReadingIntervals,回應就會包含 RouteLegTravelAdvisory 下的路線段層級路況資料。

X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline,routes.travelAdvisory.speedReadingIntervals,routes.legs.polyline.encodedPolyline,routes.legs.travelAdvisory.speedReadingIntervals

如要進一步瞭解如何指定回應欄位遮罩,請參閱「選擇要傳回的欄位」。

回應範例

只要透過欄位遮罩要求 speedReadingIntervals,系統就會在 routes.travelAdvisory.speedReadingIntervals 下方填入這些欄位。routes.legs.travelAdvisory.speedReadingIntervals 下方會顯示航段層級流量。每個間隔都會由 startPolylinePointIndex、endPolylinePointIndex 和對應的速度類別描述。請注意,根據 proto3 做法,如果區間內沒有起始索引,則對應至索引 0。

{
  "routes": [
    {
      "legs": {
        "polyline": {
          "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD"
        },
        "travelAdvisory": {
          "speedReadingIntervals": [
            {
              "endPolylinePointIndex": 1,
              "speed": "NORMAL"
            },
            {
              "startPolylinePointIndex": 1,
              "endPolylinePointIndex": 2,
              "speed": "SLOW"
            },
            {
              "startPolylinePointIndex": 2,
              "endPolylinePointIndex": 4,
              "speed": "NORMAL"
            }
          ] 
        }
      },
      "polyline": {
        "encodedPolyline": "}boeF~zbjVAg@EmB`GWHlD"
      },
      "travelAdvisory": {
        "speedReadingIntervals": [
          {
            "endPolylinePointIndex": 1,
            "speed": "NORMAL"
          },
          {
            "startPolylinePointIndex": 1,
            "endPolylinePointIndex": 2,
            "speed": "SLOW"
          },
          {
            "startPolylinePointIndex": 2,
            "endPolylinePointIndex": 4,
            "speed": "NORMAL"
          }
        ] 
      }
    }
  ]
}

使用 Maps SDK 轉譯路況感知折線

建議您使用 Google Maps SDK 提供的各種功能,在地圖上顯示車流感知折線,包括自訂顏色、筆劃,以及沿著折線延伸的圖案。如要進一步瞭解如何使用折線,請參閱「Android 版折線功能」和「iOS 版折線功能」。

折線算繪範例

Maps SDK 使用者可以定義自訂對應邏輯,連結速度類別和折線算繪結構定義。舉例來說,使用者可以決定在地圖上以藍色粗線顯示「正常」速度,以橘色粗線顯示「慢」速度,依此類推。

下列程式碼片段會針對墨爾本至伯斯的測地線段,加上粗的藍色折線。詳情請參閱「自訂外觀」(Android) 和「自訂折線」(iOS)。

Android

Java

Polyline line = map.addPolyline(new PolylineOptions()
    .add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734))
    .width(25)
    .color(Color.BLUE)
    .geodesic(true));

Kotlin

val line: Polyline = map.addPolyline(
  PolylineOptions()
    .add(LatLng(-37.81319, 144.96298), LatLng(-31.95285, 115.85734))
    .width(25f)
    .color(Color.BLUE)
    .geodesic(true)
)

iOS

Objective-C

GMSMutablePath *path = [GMSMutablePath path];
[path addLatitude:-37.81319 longitude:144.96298];
[path addLatitude:-31.95285 longitude:115.85734];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 10.f;
polyline.strokeColor = .blue;
polyline.geodesic = YES;
polyline.map = mapView;

Swift

let path = GMSMutablePath()
path.addLatitude(-37.81319, longitude: 144.96298)
path.addLatitude(-31.95285, longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 10.0
polyline.geodesic = true
polyline.map = mapView