結合路線摘要與沿途搜尋功能

您可以將路線摘要計算與沿途搜尋結合使用。 在這種情況下,Text Search (新版) 會在回應中傳回前往每個地點的旅行時間和距離,以及從每個地點到路線最終目的地的旅行時間和距離。

如要使用「文字搜尋 (新版)」計算路徑摘要,並沿途搜尋:

  1. 使用 Routes API 計算路徑,在回應中傳回路徑折線

  2. 使用 searchAlongRouteParameters.setPolyline() 將路線折線傳遞至 Text Search (New),讓搜尋結果偏向路線。回覆會顯示符合搜尋條件,且位於指定路線附近的景點。

  3. 建構要求物件時,請新增 .setRoutingSummariesIncluded(true)

根據預設,文字搜尋 (新版) 會沿著整條路線執行搜尋:

// Define the route polyline object using the route string.
EncodedPolyline encodedPolyline = EncodedPolyline.newInstance("wblcFptchVIFOd@G@EVw@Ms@dHKR}ApNA`AF~@Hf@TjAb@bBb@~@n@p@^Rd@~@Vz@HVz@nDLt@?d@Kr@c@~@mD`G?`@aEfGkCnDuChDm`@bb@[`@{GhHeEdEciBnnBkC`DkC~DaClEuKjT_Z|l@Qb@iR~_@}EzJ_AdB_Und@kAfCaOjZkg@vcAqBzD_]rr@iBlEaBxEgArD}AlG}AhHsA`IeAnH{@dIq@dJgL~iBq@rHu@vGgAtHwArHaBhHkBzG_DpJ}Nbc@iBhGkA|EgC|LcIjb@oAhG_AvDgAdDkApC_BzCiBpCsFvGii@vn@scAxlAmLjNgSzUeRjT{TzWqExEmG|FuNlMmMhLaRvPqOlNmbAl}@mFlF{PlOmJfIoElE}LtMiSbU_H`I}}@jcAwl@vp@oAbBqA~BeAhCm@tBg@fCWrBQ~BI|DaB~rBO~D[bEa@`Dm@pDaAdE{@vC_BbEkB~Def@|z@sEzHKJeS~]}K`S{\\~l@cXpe@sBpDm@bAuCxDkBrBiC~BwCtByBnAcBx@}Bt@{Bn@gh@|LaOpDeFhAoDj@aE^kVrA_E^iEr@yD~@uBr@gMjF_EnAcCh@eFr@_DRsAD}@Jsu@xCWDqIV}BCeCOyDm@cBa@_DmA}JeE_CwAsBcBiBoBuAqBmOoX{CuEkB_CoDqDkVoUoD{CeE_DkEkC_FeCqB}@sDuAoDgAeCe@cCW}CK}BDaDTeOlBcuBrYaNlBq@Dyd@rGyFt@yBb@eBf@oCnAoBlAkIpGkAp@wBbAaCt@oFdAwKjBoGxA{FbByIjC_HfB_@KmNdDuC|@uFzBcH|C{@\\[?sBv@}@VaBVoA@y@EmAQcA[w@]aBkAeAkA}BuDUKs@uAqBsCwBcCgAiAiN_MyKsJsG{GkBaBiBuA{BwAwDkBcOaHiC_AiCg@}BQcCAcBHqBVkB`@qEjAu@LgCVgAHwG@sG?mABsH^eNr@mBXy@NqBt@uAt@aBlAkAlA}BtCyApBiAdB_BxB{A`B}@j@oAf@s@PeCVcIf@gAAkAQy@YiAo@_A{@_DgEgJqM_DeEaM}PoBiCzAsBw@kAdAGVk@f@q@z@C
");

// Specify the list of fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define the search along route parameters object and pass the polyline object.
SearchAlongRouteParameters searchAlongRouteParameters = SearchAlongRouteParameters.builder()
    .setPolyline(encodedPolyline)
    .build();

// Use the builder to create a SearchByTextRequest object and pass the search along route parameters.
final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food", placeFields)
    .setMaxResultCount(10)
    .setSearchAlongRouteParameters(searchAlongRouteParameters)
    .setRoutingSummariesIncluded(true)
    .build();

// Call PlacesClient.searchByText() to perform the search.
// Define a response handler to process the returned List of Place objects.
placesClient.searchByText(searchByTextRequest)   
    .addOnSuccessListener(response -> {
      List<Place> places = response.getPlaces();
      List<RoutingSummary> routingSummaries = response.getRoutingSummaries();
      List<Leg> legs = routingSummaries.get(0).getLegs();
      Duration duration = legs.get(0).getDuration();
    });

回應包含的物件會列出所要求地點的欄位,以及前往每個地點的所需時間和距離等路線摘要。您可以呼叫 SearchByTextResponse.getRoutingSummaries(),傳回路徑摘要清單。

針對 legs 陣列中的每個項目,Text Search (New) 會傳回「兩段式」行程時間:

  • 第一個路段包含從出發地到該地點的旅行時間和距離。

  • 第二個路段包含從該地點到路線目的地的旅行時間和距離。

指定路線起點、交通方式和路線修飾符

您可以指定路線起點、交通方式、路線修飾符和路線偏好設定,修改搜尋和路線摘要計算結果。如要計算路線摘要,但未指定路線,則交通方式和路線修飾符的運作方式與「指定交通方式」主題所述相同。

根據預設,每個結果的第一段都包含從折線定義的原點到每個地點的距離。不過,您可以在要求中明確指定路由來源,藉此覆寫預設值。如要指定,所有回應的第一個路段會指定從指定路徑起點的距離和時間,並覆寫折線的起點。

在下一個範例中,您將路線起點指定為加州聖馬特奧的座標,並指定避開收費路段,以及將結果數設為 5:

// Define the route polyline object using the route string.
EncodedPolyline encodedPolyline = EncodedPolyline.newInstance("wblcFptchVIFOd@G@EVw@Ms@dHKR}ApNA`AF~@Hf@TjAb@bBb@~@n@p@^Rd@~@Vz@HVz@nDLt@?d@Kr@c@~@mD`G?`@aEfGkCnDuChDm`@bb@[`@{GhHeEdEciBnnBkC`DkC~DaClEuKjT_Z|l@Qb@iR~_@}EzJ_AdB_Und@kAfCaOjZkg@vcAqBzD_]rr@iBlEaBxEgArD}AlG}AhHsA`IeAnH{@dIq@dJgL~iBq@rHu@vGgAtHwArHaBhHkBzG_DpJ}Nbc@iBhGkA|EgC|LcIjb@oAhG_AvDgAdDkApC_BzCiBpCsFvGii@vn@scAxlAmLjNgSzUeRjT{TzWqExEmG|FuNlMmMhLaRvPqOlNmbAl}@mFlF{PlOmJfIoElE}LtMiSbU_H`I}}@jcAwl@vp@oAbBqA~BeAhCm@tBg@fCWrBQ~BI|DaB~rBO~D[bEa@`Dm@pDaAdE{@vC_BbEkB~Def@|z@sEzHKJeS~]}K`S{\\~l@cXpe@sBpDm@bAuCxDkBrBiC~BwCtByBnAcBx@}Bt@{Bn@gh@|LaOpDeFhAoDj@aE^kVrA_E^iEr@yD~@uBr@gMjF_EnAcCh@eFr@_DRsAD}@Jsu@xCWDqIV}BCeCOyDm@cBa@_DmA}JeE_CwAsBcBiBoBuAqBmOoX{CuEkB_CoDqDkVoUoD{CeE_DkEkC_FeCqB}@sDuAoDgAeCe@cCW}CK}BDaDTeOlBcuBrYaNlBq@Dyd@rGyFt@yBb@eBf@oCnAoBlAkIpGkAp@wBbAaCt@oFdAwKjBoGxA{FbByIjC_HfB_@KmNdDuC|@uFzBcH|C{@\\[?sBv@}@VaBVoA@y@EmAQcA[w@]aBkAeAkA}BuDUKs@uAqBsCwBcCgAiAiN_MyKsJsG{GkBaBiBuA{BwAwDkBcOaHiC_AiCg@}BQcCAcBHqBVkB`@qEjAu@LgCVgAHwG@sG?mABsH^eNr@mBXy@NqBt@uAt@aBlAkAlA}BtCyApBiAdB_BxB{A`B}@j@oAf@s@PeCVcIf@gAAkAQy@YiAo@_A{@_DgEgJqM_DeEaM}PoBiCzAsBw@kAdAGVk@f@q@z@C
");

// Specify the list of fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define the routing modifiers object.
RouteModifiers routeModifiers = RouteModifiers.builder()
    .setAvoidTolls(true)
    .build();

// Define the routing parameters object and pass the routing origin.
// Set the travel mode to DRIVE.
// Pass the routeModifiers object.
RoutingParameters routingParameters = RoutingParameters.builder()
    .setOrigin(toLatLng("37.56617, -122.30870"))
    .setTravelMode(DRIVE)
    .setRouteModifiers(routeModifiers)
    .build();

// Define the search along route parameters object and pass the polyline object.
SearchAlongRouteParameters searchAlongRouteParameters = SearchAlongRouteParameters.builder()
    .setPolyline(encodedPolyline)
    .build();

// Use the builder to create a SearchByTextRequest object and pass the search along route parameters.
final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food", placeFields)
    .setMaxResultCount(5)
    .setRoutingParameters(routingParameters)
    .setSearchAlongRouteParameters(searchAlongRouteParameters)
    .setRoutingSummariesIncluded(true)
    .build();

// Call PlacesClient.searchByText() to perform the search.
// Define a response handler to process the returned List of Place objects.
placesClient.searchByText(searchByTextRequest)   
    .addOnSuccessListener(response -> {
      List<Place> places = response.getPlaces();
      List<RoutingSummary> routingSummaries = result.getRoutingSummaries();
      List<Leg> legs = routingSummaries.get(0).getLegs();
      Duration duration = legs.get(0).getDuration();
    });

下圖顯示的地圖包含路線折線、新的起點 (淺藍色圖釘) 和搜尋結果中的地點 (綠色圖釘)。請注意,所有結果都位於路線上,但都經過聖馬特奧:

更新來源的搜尋結果。