沿路线搜索
如需使用文本搜索(新版)沿路线搜索,请在请求中将 Routes API 中预计算的路线作为多段线传递。响应包含与搜索条件匹配且位于指定路线附近的地点。
如需沿路线搜索,请执行以下操作:
-
使用 Routes API 计算路线,并在响应中返回路线的编码多段线。
-
使用
searchAlongRouteParameters.polyline.encodedPolyline
参数将路线的编码多段线传递给文本搜索(新),以便将搜索结果偏向于路线。然后,响应中会包含与搜索条件匹配且位于指定路线附近的地点。
默认情况下,“文本搜索(新)”会沿着多段线定义的起点到目的地的整个路线执行搜索。在以下示例中,多段线定义了以下路线:
- 发货地:1600 Amphitheatre Parkway, Mountain View, CA
- 目的地:24 Willie Mays Plaza, San Francisco, CA
此行程对此路线的编码多段线由以下字符串表示:
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
以下示例使用路线的编码多段线来偏向搜索结果:
curl -X POST -d '{ "textQuery" : "Spicy Vegetarian Food", "searchAlongRouteParameters": { "polyline": { "encodedPolyline": "ROUTE_POLYLINE" } } }' \ -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \ -H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel' \ 'https://places.googleapis.com/v1/places:searchText'
响应包含与搜索条件匹配且偏向于相应路线的地点:
{ "places": [ { "formattedAddress": "60 Morris St, San Francisco, CA 94107, USA", "priceLevel": "PRICE_LEVEL_MODERATE", "displayName": { "text": "Umami Express", "languageCode": "en" } }, { "formattedAddress": "1130 4th St, San Francisco, CA 94158, USA", "priceLevel": "PRICE_LEVEL_MODERATE", "displayName": { "text": "House of Tadu Ethiopian Kitchen", "languageCode": "en" } }, { "formattedAddress": "1602 El Camino Real Ste A, Belmont, CA 94002, USA", "priceLevel": "PRICE_LEVEL_MODERATE", "displayName": { "text": "Eats Meets West Bowls", "languageCode": "en" } }, /.../ ] }
您无需将结果偏向于整个路线。例如,您在执行搜索时可能已经走到了路线的一半。在这种情况下,您可以向搜索指定一个显式路由起点,以替换在编码多段线中定义的起点。在以下示例中,您可以指定加利福尼亚州圣马特奥的纬度和经度坐标,该坐标位于路线多段线的大约中间位置:
curl -X POST -d '{ "textQuery" : "Spicy Vegetarian Food", "searchAlongRouteParameters": { "polyline": { "encodedPolyline": "ROUTE_POLYLINE" } }, "routingParameters": { "origin": { "latitude": 37.56617, "longitude": -122.30870 } } }' \ -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \ -H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel' \ 'https://places.googleapis.com/v1/places:searchText'