將路線摘要與沿途搜尋功能結合
您可以結合路徑摘要計算結果,並沿路搜尋。在這種情況下,Text Search (新版) 會在回應中傳回前往每個地點的時間和距離,以及從每個地點到路線最終目的地的距離。
如何使用文字搜尋 (新版) 計算路線摘要,以及沿路搜尋:
-
使用 Routes API 計算路線,在回應中傳回路線折線。
-
使用
searchAlongRouteParameters.setPolyline()
將路線折線傳遞至 Text Search (新版),將搜尋結果自訂調整至路線。回應就會包含符合搜尋條件且位於指定路線附近的地點。 -
建構要求物件時,請新增
.setRoutingSummariesIncluded(true)
。
根據預設,Text Search (新版) 會沿著整個路線執行搜尋:
// 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 (新版) 會傳回兩段航程的旅遊時間:
-
第一段包含從出發地到目的地的行程時間和距離。
-
第二段則包含從該地點到路線目的地的行程時間和距離。
指定路徑起點、交通方式和路線修改項目
您可以指定路徑起點、交通方式、路徑修改項目和路徑偏好設定,修改搜尋和路徑摘要計算結果。交通方式和路線修飾符的運作方式,與在不指定路線的情況下,不指定路線,如「指定交通選項」主題所示。
根據預設,每個結果的第一段路線都包含折線定義與每個地點的起點距離。但是,您可以在要求中明確指定轉送來源,以覆寫此預設值。如有指定,所有回應的第一段就會指定與指定路由起點的距離和所需時間,進而覆寫折線的起點。
在下一個範例中,您將指定路線起點做為加州聖馬特奧的座標,指定避開收費路段,並將結果數量設為 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(); });
下圖顯示的地圖包含路線折線、新起點 (淺藍色圖釘),以及搜尋結果中的地點 (綠色圖釘)。請注意,所有結果都位於路線上,但已過了聖馬刁: