這項產品或功能為舊版。如要進一步瞭解舊版狀態,請參閱「
舊版產品和功能」。如要遷移至 iOS 版 Places API(新版),請參閱
遷移指南。
工作階段符記
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Place Autocomplete (舊版) 會使用工作階段符記,將使用者自動完成搜尋的查詢和選取階段歸入不同的工作階段,以用於計費。工作階段是從使用者輸入查詢時開始,到使用者選取地點並發出「Place Details (Legacy)」呼叫時結束。在每個工作階段中,使用者可以輸入多筆 Autocomplete 查詢,最終選擇一個地點。在單一工作階段中,用於每項要求的 API 鍵必須屬於同一個 Google Cloud 控制台專案。工作階段結束後,符記就會失效;您的應用程式必須為每個工作階段產生新的符記。如果省略 sessiontoken
參數或重複使用工作階段符記,系統會視為未提供工作階段符記,並針對工作階段收費 (每個要求分別收費)。
建議您遵循下列準則:
- 在所有自動完成工作階段中使用工作階段符記。
- 為每個工作階段產生新的符記。
- 請確認在工作階段中,所有地點自動完成 (舊版) 和地點詳細資料 (舊版) 要求使用的 API 金鑰都屬於同一個 Google Cloud 控制台專案。
- 請務必針對每個新的工作階段傳遞不重複的工作階段符記。如果多個工作階段使用同一個符記,則每個要求會分別計費。
您可以選擇從要求中省略自動完成工作階段符記。如果省略工作階段符記,系統會針對每項要求分別收費,並觸發 Autocomplete - Per Request SKU。假如您重複使用工作階段符記,系統會將工作階段視為無效,並針對相關要求收費 (因為系統會將其視為未提供工作階段符記)。
範例
使用者輸入查詢時,系統會每隔幾個按鍵 (不是每個字元) 呼叫一次自動完成要求,並傳回可能的結果清單。當使用者從結果清單中選取項目時,系統會將該選取項目視為一項要求,並將搜尋期間提出的所有要求合併計算為單一要求。如果使用者選取地點,系統就會免費提供搜尋查詢,只會收取地點資料要求的費用。如果使用者在工作階段開始後的幾分鐘內未選取任何項目,系統只會收取搜尋查詢費用。
讓我們從應用程式的角度來檢視這個事件流程。
- 使用者開始輸入查詢,搜尋「巴黎 (法國)」。
- 在偵測到使用者輸入內容後,應用程式會建立新的「Token A」工作階段符記。
- 使用者輸入內容時,API 會每隔幾個字元發出自動完成要求,並為每個字元顯示新的可能結果清單:
"P"
"Par"
"Paris,"
"Paris, Fr"
- 使用者選取項目時:
- 查詢產生的所有要求會分組,並以單一要求的形式新增至由「Token A」代表的工作階段。
- 使用者的選項會計為 Place Detail 要求,並新增至由「Token A」代表的工作階段。
- 工作階段結束,應用程式會捨棄「Token A」。
如要進一步瞭解 Place Autocomplete (舊版) 要求的計費方式,請參閱「用量與計費」一文。
建立工作階段符記
如要建立工作階段符記,請呼叫 GMSAutocompleteSessionToken.init()
。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間: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)."]]