計算路由摘要

如要使用 Text Search (新版)Nearby Search (新版) 來計算回應中各個地點的交通時間和距離:

  1. 在要求中傳遞 routingParameters.origin 參數,指定路徑起點的經緯度座標。這個參數是 計算每個地點的所需時間和距離 回應。

  2. 在欄位遮罩中加入 routingSummaries,讓回應包含 routingSummaries 陣列。這個陣列包含路線起點到回應中各個地點的時間和距離。

使用 Text Search (新版)

在下列要求中,您會計算前往各航點所需的時間和距離 Place Search (新版) 回應中的地點:

  curl -X POST -d '{
    "textQuery" : "Spicy Vegetarian Food in Sydney, Australia",
    "routingParameters": {
      "origin": {
        "latitude": -33.8688,
        "longitude": 151.1957362
      }
    }
  }' \
  -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \
  -H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel,routingSummaries' \
  'https://places.googleapis.com/v1/places:searchText'

回應包含兩個 JSON 陣列:places 陣列包含 和 routingSummaries 陣列,其中包含目的地的所需時間和距離 旅行至每個位置:

{
  "places": [
    {
      object (Place)
    }
  ]
  "routingSummaries": [
    {
      "legs": [
        object (Leg)
      ]
    }
  ]
}

routingSummaries 陣列中的每個元素都位於對應的陣列 做為 places 陣列中的地點。也就是說,routingSummaries[0] 中的元素會對應至 places[0] 中的地點。

routingSummaries 的陣列長度與 places。如果無法取得地點的 routingSummary, 陣列項目空白。

本範例計算的是 每個地點的起點,則回應中的 legs 欄位會包含單一 Leg 這個物件包含來自轉送來源的 durationdistanceMeters 移到對應位置:

{
  "places": [
    {
      "formattedAddress": "1, Westfield Sydney Central Plaza, 450 George St, Sydney NSW 2000, Australia",
      "displayName": {
        "text": "Gözleme King Sydney",
        "languageCode": "en"
      }
    },
    {
      "formattedAddress": "367 Pitt St, Sydney NSW 2000, Australia",
      "priceLevel": "PRICE_LEVEL_MODERATE",
      "displayName": {
        "text": "Mother Chu's Vegetarian Kitchen",
        "languageCode": "en"
      }
    },
    …
  ]
  "routingSummaries": [
    {
      "legs": [
        {
          "duration": "597s",
          "distanceMeters": 2607
        }
      ]
    },
    {
      "legs": [
        {
          "duration": "562s",
          "distanceMeters": 2345
        }
      ]
    },
   …
  ]
}

從這個範例中,您可以看到從路線起點到結果中第一個地點的時間和距離分別為 597 秒和 2607 公尺。

在本例中,您將計算以下地點進入各地點所需的時間和距離: Nearby Search 的回應。這個範例會搜尋澳洲雪梨的餐廳,並將位置限制和路線起點設為相同的經緯度座標:

  curl -X POST -d '{
    "includedTypes": ["restaurant"],
    "maxResultCount": 10,
    "locationRestriction": {
      "circle": {
        "center": {
          "latitude": -33.8688,
          "longitude": 151.1957362},
        "radius": 500.0
      }
    },
    "routingParameters": {
      "origin": {
        "latitude": -33.8688,
        "longitude": 151.1957362
      }
    }
  }' \
  -H 'Content-Type: application/json' -H "X-Goog-Api-Key:API_KEY" \
  -H "X-Goog-FieldMask: places.displayName,routingSummaries" \
  https://places.googleapis.com/v1/places:searchNearby

您不必為 locationRestriction 和路徑起點使用相同的座標。舉例來說,您可以將 locationRestriction 設為雪梨的中心點,讓搜尋結果偏向該圓圈。但接下來 路線起點到房屋座標,意思是不同地點 。要求隨後會將搜尋結果自訂調整為 並根據您所在的位置 房子。

指定交通選項

根據預設,系統會以汽車為基準計算時間和距離。不過, 可以在搜尋時控制交通工具類型以及其他選項。

  • 使用 routingParameters.travelMode 參數來設定 搭乘大眾運輸工具到DRIVEBICYCLEWALKTWO_WHEELER。如要進一步瞭解這些選項,請參閱「路線可用的車輛類型」。

  • 使用 routingParameters.routingPreference 屬性設定轉送 設為 TRAFFIC_UNAWARE (預設)、TRAFFIC_AWARETRAFFIC_AWARE_OPTIMAL。每個選項的資料品質和延遲時間都不同。詳情請參閱「指定是否要納入流量資料的方式」。
  • 使用 routingParameters.routeModifiers 屬性指定 avoidTollsavoidHighwaysavoidFerriesavoidIndoor。如要 如要瞭解這些選項,請參閱指定路線地圖項目, 請避免。

在下例中,您將交通方式指定為 DRIVE,以避免 高速公路:

curl -X POST -d '{
  "textQuery" : "Spicy Vegetarian Food in Sydney, Australia",
  "routingParameters": {
    "origin": {
      "latitude": -33.8688,
      "longitude": 151.1957362
    },
    "travelMode":"DRIVE",
    "routeModifiers": {
      "avoidHighways": true
    }
  }
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \
-H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel,routingSummaries' \
'https://places.googleapis.com/v1/places:searchText'