将路线摘要与沿路线搜索结合使用
您可以将路线摘要计算与沿路线搜索结合使用。在这种情况下,文本搜索(新)会在响应中返回到每个地点的旅行时长和距离,然后返回从每个地点到路线最终目的地的距离。
如需使用文本搜索(新版)计算路线摘要以及沿路线搜索,请执行以下操作:
-
使用 Routes API 计算路线,并在响应中返回路线多段线。
-
使用
searchAlongRouteParameters.setPolyline()
将路线多段线传递给文本搜索(新版),以便将搜索结果偏向于路线。然后,响应中会包含与搜索条件匹配且位于指定路线附近的地点。 -
构建请求对象时,请添加
.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
数组中的每个条目,文本搜索(新)都会返回两段式行程时间:
-
第一段包含从起点到地点的旅行时长和距离。
-
第二段包含从相应地点到路线目的地的行程时长和距离。
指定路由起点、出行方式和路线修饰符
您可以通过指定路线起点、出行方式、路线修饰符和路线偏好设置来修改搜索和路线摘要计算方式。如指定出行方式主题中所示,在计算路线摘要时,不指定路线时,行程模式和路线修饰符的运作方式相同。
默认情况下,每个结果的第一段包含多段线所定义的起点到每个地点的距离。不过,您可以通过在请求中明确指定路由来源来替换该默认值。如果指定,则所有响应的第一个路段都会指定相对于指定路线起点的距离和用时,从而替换多段线起点。
在下例中,您将路线起点指定为加利福尼亚州圣马特奥的坐标,指定要避开收费站,并将结果数量设置为 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(); });
下图显示了一张地图,其中包含路线多段线、新起点(浅蓝色图钉)和搜索结果中的地点(绿色图钉)。请注意,所有结果均位于相应路线上,但都位于圣马特奥之后: