此产品或功能处于旧版状态。如需详细了解“旧版”状态,请参阅
旧版产品和功能。如需迁移到 Places API for iOS(新版),请参阅
迁移指南。
会话令牌
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
“地点自动补全”(旧版)使用会话令牌将用户自动补全搜索的查询和选择阶段归入不同的会话,以便进行结算。会话在用户开始输入查询内容时开始,并在用户选择地点并执行对“地点详情(旧版)”的调用时结束。在每个会话中,用户可以输入多项自动补全查询,并最终选择一个地点。在一个会话中,每个请求使用的 API 密钥都必须属于同一 Google Cloud 控制台项目。会话结束后,令牌将失效;您的应用必须为每个会话生成一个新的令牌。如果您省略 sessiontoken
参数或重复使用会话令牌,系统会按未提供会话令牌的情况为会话计费(每个请求均单独结算)。
我们建议您遵循以下准则:
- 针对所有自动补全会话使用会话令牌。
- 为每个会话生成一个新的令牌。
- 确保为会话中的所有“地点自动补全(旧版)”和“地点详情(旧版)”请求使用的 API 密钥属于同一 Google Cloud 控制台项目。
- 请务必为每个新会话传递唯一的会话令牌。针对多个会话使用同一令牌会导致每个请求被单独计费。
您可以选择从请求中省略自动补全会话令牌。如果省略会话令牌,系统会对每个请求单独结算费用,从而触发自动补全 - 按请求结算 SKU。如果您重复使用某个会话令牌,相应会话会被视为无效,并且系统会按未提供会话令牌的情况为请求计费。
示例
当用户输入查询时,系统会每隔几次按键(而非按字符)调用一次自动补全请求,并返回可能的结果列表。当用户从结果列表中进行选择时,该选择会计为一次请求,并且搜索期间发出的所有请求都会打包在一起,计为一次请求。如果用户选择某个地点,则搜索查询是免费的,只有地点数据请求需要付费。如果用户在会话开始后的几分钟内未做出选择,系统只会收取搜索查询费用。
我们从应用的角度来审视这一事件流程。
- 用户开始输入查询,搜索“法国巴黎”。
- 检测到用户输入后,应用会创建一个新的会话令牌“令牌 A”。
- 用户输入字符时,API 会每输入几个字符就发出一次自动补全请求,并针对每个字符显示一个新的潜在结果列表:
“P”
“Par”
“Paris,”
“Paris, Fr”
- 当用户进行选择时:
- 查询产生的所有请求都会分组,并作为单个请求添加到由“令牌 A”表示的会话中。
- 系统会将用户的选择计为一次地点详情请求,并将其添加到由“令牌 A”表示的会话中。
- 会话结束,应用舍弃“令牌 A”。
如需详细了解“地点自动补全(旧版)”请求的结算方式,请参阅使用量和结算。
创建会话令牌
如需创建会话令牌,请调用 GMSAutocompleteSessionToken.init()
。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Session Tokens\n\nSelect platform: [Android](/maps/documentation/places/android-sdk/session-tokens \"View this page for the Android platform docs.\") [iOS](/maps/documentation/places/ios-sdk/session-tokens \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/places-autocomplete#session_tokens \"View this page for the JavaScript platform docs.\") [Web Service](/maps/documentation/places/web-service/session-tokens \"View this page for the Web Service platform docs.\")\n\nPlace Autocomplete (Legacy) uses session tokens to group the query and selection\nphases of a user autocomplete search into a discrete session for billing\npurposes. The session begins when the user starts typing a query, and concludes\nwhen they select a place and a call to Place Details (Legacy) is made. Each session can\nhave multiple autocomplete queries, followed by one place selection. The API\nkey(s) used for each request within a session must belong to the same\nGoogle Cloud console project. Once a session has concluded, the token is no longer\nvalid; your app must generate a fresh token for each session. If the\n`sessiontoken` parameter is omitted, or if you reuse a session\ntoken, the session is charged as if no session token was provided (each request\nis billed separately).\n\nWe recommend the following guidelines:\n\n- Use session tokens for all autocomplete sessions.\n- [Generate a fresh token](#create-session-tokens) for each session.\n- Ensure that the API key(s) used for all Place Autocomplete (Legacy) and Place Details (Legacy) requests within a session belong to the same Google Cloud console project.\n- Be sure to pass a unique session token for each new session. Using the same token for more than one session will result in each request being billed individually.\n\nYou can optionally omit the autocomplete session token from a request. If\nthe session token is omitted, each request is billed separately, triggering the\n\n[Autocomplete - Per Request](/maps/billing-and-pricing/sku-details#autocomplete-request-new-ess-sku)\n\n\nSKU. If you reuse a session token, the session is considered invalid and the\nrequests are charged as if no session token was provided.\n\nExample\n-------\n\nAs the user types a query, an autocomplete request is called every few\nkeystrokes (not per-character), and a list of possible results is returned.\nWhen the user makes a selection from the result list, the selection counts as\na request, and all of the requests made during the search are bundled and\ncounted as a single request. If the user selects a place, the search query is\navailable at no charge, and only the Place data request is charged. If the user does not make a\nselection within a few minutes of the beginning of the session, only the\nsearch query is charged.\n\nLet's examine this flow of events from the perspective of an app.\n\n1. A user begins typing a query to search for \"Paris, France\".\n2. Upon detecting user input, the app creates a new session token, \"Token A\".\n3. As the user types, the API makes an autocomplete request every few characters, displaying a new list of potential results for each: \n \"P\" \n \"Par\" \n \"Paris,\" \n \"Paris, Fr\" \n4. When the user makes a selection:\n - All requests resulting from the query are grouped and added to the session represented by \"Token A\", as a single request.\n - The user's selection is counted as a Place Detail request, and added to the session represented by \"Token A\".\n5. The session is concluded, and the app discards \"Token A\".\n\nFor more information about how Place Autocomplete (Legacy) requests are billed, see\n\n[Usage and Billing](/maps/documentation/places/ios-sdk/usage-and-billing).\n\nCreate session tokens\n---------------------\n\n\nTo create a session token, call\n[`GMSAutocompleteSessionToken.init()`](/maps/documentation/places/ios-sdk/reference/interface_g_m_s_autocomplete_session_token)."]]