選擇 SDK 版本

應用程式提供的 Places SDK for Android 功能取決於您在應用程式設定中指定的 SDK 版本號碼 (例如 3.5.0)、在 API 金鑰上啟用的 API,以及初始化應用程式的方式。本指南說明不同版本之間的差異,以及如何選取要在應用程式中啟用的版本。

SDK 版本號碼

您在應用程式層級 build.gradle 檔案中指定的 SDK 版本號碼,會決定應用程式是否可存取特定版本中新增的功能。舉例來說,Autocomplete (新版) 僅適用於 SDK 3.5.0 以上版本。

如要進一步瞭解每個 SDK 版本推出的變更,請參閱 Places SDK for Android 版本資訊

Places SDK for Android 的 Places API 版本

除了指定 SDK 版本號碼外,您也必須在 Google Cloud 控制台中選取 SDK 呼叫的 Places API 服務:Places API 或 Places API (新版)。除了 SDK 版本號碼外,您選取的 API 服務也會決定應用程式可用的特定功能版本,例如 Autocomplete 或 Autocomplete (新版)。雖然您只能指定一個 SDK 版本號碼,但可以同時為 API 金鑰啟用 Places API 和 Places API (新版),接著透過您在應用程式中初始化 SDK 的方式,選取 SDK 呼叫的服務。不過,在大多數情況下,建議您啟用 Places API (新版) 並使用 (新版) 在應用程式中使用 SDK 功能。

選取 SDK 版本和 API 服務

如要選取應用程式可用的 SDK 功能,請按照下列步驟操作:

  1. 在 Google Cloud 專案中啟用 Places API 和/或 Places API (新版)。詳情請參閱啟用 API 一文
  2. 在 API 金鑰限制中,啟用 Places API 和/或 Places API (新版)。詳情請參閱「限制 API 金鑰」一文。
  3. 在應用程式層級的 build.gradle 檔案中,指定版本號碼。詳情請參閱「安裝」。

  4. 呼叫 Places.initializeWithNewPlacesApiEnabled()Places.initialize() 方法,將應用程式初始化。

如要進一步瞭解如何選取 Places API 服務,請參閱「設定 Google Cloud 專案」一文。

各版本提供的 SDK 功能

下表列出每個 SDK 功能所需的 SDK 和 API 版本:

特徵 API 金鑰已啟用 Places API 初始化方法 SDK 最低版本
自動完成 (新版) Places API (新推出) initializeWithNewPlacesApiEnabled() 3.5.0
Place Details (新功能) Places API (新推出) initializeWithNewPlacesApiEnabled() 3.3.0
Nearby Search (新版) Places API (新推出) initializeWithNewPlacesApiEnabled() 3.5.0
Place Photos (新推出) Places API (新推出) initializeWithNewPlacesApiEnabled() 3.4.0
Text Search (新版) Places API (新推出) initializeWithNewPlacesApiEnabled() 3.3.0
Place Autocomplete Places API initialize()
目前所在地點 Places API initialize()
Place Details Places API initialize()
Place Photo Places API initialize()

選擇要啟用的版本時應考量的事項

您可以參考下列原則來決定要選擇哪個版本:

  • 如果您是剛開始使用 Places SDK for Android 的新客戶,請先採用 Places API (新版) 和新版 SDK。
  • 如果您是現有客戶,可以繼續使用現有的 SDK。不過,如要善用 Places SDK for Android (新版) 的效能改善和功能強化功能,建議您使用新的 SDK。如要遷移至新的 SDK,您不需要進行遷移,只要按照「選取 SDK 版本和 API 服務」一節的步驟操作即可。
  • Places SDK for Android (新版) 自 3.5.0 版起開始提供工作階段符記。詳情請參閱「工作階段符記」。
  • 使用 Place Autocomplete 小工具發出的呼叫一律會呼叫 Place Autocomplete,而非 Place Autocomplete (新版);小工具呼叫不受初始化方法影響。
  • Place Photos (新版) 只會傳回點陣圖圖片的 URI,Place Photos 則只會傳回點陣圖圖片。
  • Current Place 僅可在 Places SDK for Android 中使用,不適用於 Places SDK for Android (新版)。

Places SDK for Android 中的強化功能 (新推出)

本節說明 Places SDK for Android 新增的重要功能 (新功能)。

實作於 Google Cloud 標準平台

Places SDK for Android (新版) 已經在 Google Cloud 的服務基礎架構中實作。這項實作能夠提供更加安全可靠的平台。這項標準設計可讓 SDK 維持一定程度的一致性,並透過 Places SDK for Android (新功能) 提高開發效率。

提高效能

Places SDK for Android (新版) 提供更完善的效能,因此值得考慮取代使用現有 SDK 的應用程式。

新功能

Places SDK for Android (新版) 包含所有 SDK 功能的最新版本:

新版 Text Search 服務

Text Search (新版) 可根據字串 (例如「台南魯肉飯」、「西門町附近的鞋店」或「中正路 123 號」),傳回一組地點的相關資訊。這項服務會傳回與文字字串及位置自訂調整設定相符的地點清單。

在 Placed Details (新版) 和 Place Photos (新功能) 中加入新的回應資料

  • Place Details (新版) 現在在回應 Place 物件中加入新的 Review 類別。Place 類別包含支援這個欄位的新 getReviews() 方法。呼叫 getReviews() 即可傳回地點最多 5 則評論。

  • Place Photo (新增) 會將 AuthorAttributions 新增至 PhotoMetadata 類別。AuthorAttributions 包含 AuthorAttribution 物件的 List

已在 Place Photos 中新增 URI 回應 (新)

您現在可以使用 Place Photo (新版) 傳回圖片點陣圖的 URI。先前,您只能傳回圖片點陣圖本身。

簡化計費模式

Places SDK for Android 的定價簡化了,您只需為實際使用的資料量付費。簡便的定價是使用欄位清單 (也稱為「欄位遮罩」) 進行實作。

透過 Place Details 和 Text Search,您可以使用欄位清單控管要在回應中傳回的欄位清單。您只需為要求的資料付費。使用欄位清單是個不錯的設計做法,可確保您不會要求不必要的資料,這有助於避免不必要的處理時間和計費費用。

如需這兩個 SDK 的詳細定價資訊,請參閱「用量與計費」一文。

展開的地點類型

新版 SDK 會新增下表顯示的地點類型。這些類型會在 Place Details 和 Text Search 回應中傳回。您也可以在透過 Text Search 進行搜尋時,使用這些新類型與現有類型。表 A 內含新類型。

類型
american_restaurant discount_store ice_cream_shop sandwich_shop
amusement_center dog_park indian_restaurant school_district
athletic_field electric_vehicle_charging_station indonesian_restaurant seafood_restaurant
auto_parts_store event_venue italian_restaurant ski_resort
banquet_hall extended_stay_hotel japanese_restaurant spanish_restaurant
barbecue_restaurant 設備 korean_restaurant sporting_goods_store
barber_shop 農莊民宿 lebanese_restaurant sports_club
bed_and_breakfast fast_food_restaurant Marina sports_complex
brazilian_restaurant ferry_terminal 市場 steak_house
breakfast_restaurant fitness_center medical_lab sushi_restaurant
brunch_restaurant french_restaurant mediterranean_restaurant swimming_pool
bus_stop gift_shop mexican_restaurant 裁縫師
camping_cabin golf_course middle_eastern_restaurant telecommunications_service_provider
cell_phone_store greek_restaurant 汽車旅館 thai_restaurant
child_care_agency grocery_store national_park transit_depot
chinese_restaurant guest_house park_and_ride truck_stop
coffee_shop hair_salon performing_arts_theater turkish_restaurant
community_center hamburger_restaurant pizza_restaurant vegan_restaurant
顧問 直升機 playground vegetarian_restaurant
convention_center hiking_area 學齡前 vietnamese_restaurant
木屋 historical_landmark private_guest_room visitor_center
courier_service home_improvement_store ramen_restaurant wedding_venue
cultural_center 青年旅館 resort_hotel 批發商
dental_clinic hotel rest_stop

除了這些新類型之外,Places API (新版) 也將下列類型從 Places API 的表 2 移至 Places API 的資料表 A (新版)。也就是說,您現在可以在搜尋中使用這些類型:

  • country
  • administrative_area_level_1
  • administrative_area_level_2
  • postal_code
  • locality