使用 Places 服務和 Geocoding API 搭配界線資料導向樣式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以在 Maps SDK for Android 中使用 Places 服務和 Geocoding API 搜尋區域,並取得地點的詳細資訊。Places Service 和 Geocoding API 是功能強大且穩定的替代方案,可用於取得地點 ID。如果您使用地點 ID,就能透過界線資料導向樣式重複使用這些 ID。
在 Maps SDK for Android 應用程式中加入 Places 服務和 Geocoding API 的方法如下:
使用 Places 服務
使用 Text Search (新版) 尋找地點 ID
您可以在 Places API 中使用 Text Search (新版) REST API,方法是在欄位遮罩中指定 places.id
,取得包含區域資料的地點 ID。如果僅使用 Text Search (新版) 要求地點 ID,就不會產生任何費用。瞭解詳情。
舉例來說,如要取得加州千里達的地點 ID,可以發出下列 API 呼叫:
curl -X POST -d '{
"textQuery" : "Trinidad, CA"
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \
-H 'X-Goog-FieldMask: places.id' \
'https://places.googleapis.com/v1/places:searchText'
使用 Places Autocomplete 尋找區域
Places SDK for Android 中的 Place Autocomplete 服務,可讓使用者輕鬆搜尋區域。如要將 Place Autocomplete 服務設為只傳回區域,請使用 AutocompleteSupportFragment.setTypesFilter(List)
將類型篩選器設為 PlaceTypes.REGIONS
。
取得區域的地點詳細資料
Places SDK for Android 中的地點詳細資料服務會傳回區域資料,十分實用。例如,您可以:
- 根據地點名稱搜尋界線地點 ID。
- 取得可視區域以縮放至界線。
- 取得界線的地圖項目類型 (例如
locality
)。
- 取得格式化地址,在美國區域中,這個地址會解析為「地點名稱, 州, 國家」,例如「Ottumwa, IA, USA」。
- 取得相片等其他實用資料。
使用 Geocoding API
您可以使用 Geocoding API 將地址轉換為經緯度座標和地點 ID,或將經緯度座標或地點 ID 轉換為地址。以下應用實例適合搭配界線資料導向樣式:
- 透過地理編碼取得區域的可視區域。
- 針對地理編碼呼叫進行元件篩選,取得行政區 1 到 4、縣市或郵遞區號的地點 ID。
- 透過反向地理編碼,按經緯度座標尋找地點 ID,甚至是針對特定位置的所有元件傳回地點 ID。
下列範例使用地址 (經過網址逸出處理) 向 Geocoding API 提出要求:
https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Amphitheatre%20Pkwy%20Mountain%20View%20CA&key=YOUR_API_KEY
您可以使用反向地理編碼找出地點 ID。以下地理編碼服務函式範例會傳回指定經緯度座標上所有地址元件的地點 ID:
https://maps.googleapis.com/maps/api/geocode/json?latlng=41.864182,-87.676930&key=YOUR_API_KEY
搭配元件篩選使用反向地理編碼,即可取得指定位置上一或多種下列類型的地址元件:
administrativeArea
country
locality
postalCode
下一個函式範例說明如何使用地理編碼服務,透過反向地理編碼加入元件限制,取得指定位置上的所有地址元件 (僅限 locality
類型):
https://maps.googleapis.com/maps/api/geocode/json?latlng=41.864182,-87.676930&result_type=locality&key=YOUR_API_KEY
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eYou can use the Places Service and Geocoding API with the Maps SDK for Android to find place IDs for regions and get more information about them for data-driven styling.\u003c/p\u003e\n"],["\u003cp\u003eThe Places SDK for Android, Places API, and Geocoding API can be used to search for regions and retrieve their details, including place IDs, viewport, feature type, and formatted address.\u003c/p\u003e\n"],["\u003cp\u003eText Search (New) in the Places API, Places Autocomplete in the Places SDK for Android, and the Geocoding API are methods for obtaining place IDs.\u003c/p\u003e\n"],["\u003cp\u003eWhen using these services, be aware that the Places SDK for Android, Places API, and Geocoding API require enabling and are billed separately from the Maps SDK for Android.\u003c/p\u003e\n"]]],[],null,["# Use the Places Service and Geocoding API with data-driven styling for boundaries\n\nSelect platform: [Android](/maps/documentation/android-sdk/dds-boundaries/dds-use-maps-places-apis \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-boundaries/dds-use-maps-places-apis \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-boundaries/dds-use-maps-places-apis \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nYou can use the Places Service and the\nGeocoding API with the Maps SDK for Android to search for regions, and\nget more information about places. The Places Service\nand Geocoding API are powerful and stable alternatives for obtaining\nplace IDs. If you're already using place IDs, you can reuse those IDs with\ndata-driven styling for boundaries.\n\nAdd the Places Service and Geocoding to your\nMaps SDK for Android apps in the following ways:\n\n- [Places SDK for Android](/maps/documentation/places/android-sdk) is an Android library containing methods to returns information about places.\n- [Places API](/maps/documentation/places/web-service) returns information about places using HTTP requests.\n- [Geocoder class](https://developer.android.com/reference/android/location/Geocoder) can geocode and reverse geocode dynamically from user input.\n- [Geocoding API](/maps/documentation/geocoding) lets you geocode static, known addresses.\n\n| **Note:** The [Places SDK for Android](/maps/documentation/places/android-sdk), [Places API](/maps/documentation/places/web-service), and [Geocoding API](/maps/documentation/geocoding/cloud-setup) must be enabled, and are billed separately from the Maps SDK for Android.\n\nUse the Places Service\n----------------------\n\n### Use Text Search (New) to find a place ID\n\nYou can use [Text Search (New)](/maps/documentation/places/web-service/text-search)\nREST API in the Places API to get a place ID that includes region\ndata by specifying `places.id` in the field mask. Usage of the\nText Search (New) to request place IDs only incurs\nno charge. [Learn more](/maps/documentation/places/web-service/preview-billing).\n\nFor example, to get the place ID for Trinidad, CA you can make the following\nAPI call: \n\n```\ncurl -X POST -d '{\n \"textQuery\" : \"Trinidad, CA\"\n}' \\\n-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: \u003cvar translate=\"no\"\u003eAPI_KEY\u003c/var\u003e' \\\n-H 'X-Goog-FieldMask: places.id' \\\n'https://places.googleapis.com/v1/places:searchText'\n```\n\n### Use Places Autocomplete to find regions\n\nThe [Places Autocomplete](/maps/documentation/places/android-sdk/autocomplete)\nservice in the Places SDK for Android provides a convenient way to let\nyour users search for regions. To configure the Places Autocomplete service to\nreturn only regions, use\n[`AutocompleteSupportFragment.setTypesFilter(List)`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/AutocompleteSupportFragment#setTypesFilter(java.util.List%3Cjava.lang.String%3E))\nto set the type filter to `PlaceTypes.REGIONS`.\n\n### Get place details for a region\n\nThe [Place Details](/maps/documentation/places/android-sdk/place-details)\nservice in the Places SDK for Android returns data for a region can be\nquite useful. For example, you can:\n\n- Search for boundary place IDs based on place names.\n- Get the viewport for zooming to a boundary.\n- Get the feature type for the boundary (for example `locality`).\n- Get the formatted address, which resolves to \"Place Name, State, Country\" in the United States region (for example, \"Ottumwa, IA, USA\").\n- Get other useful data such as photos.\n\nUse the Geocoding API\n---------------------\n\nThe [Geocoding API](/maps/documentation/geocoding) lets you convert\nan address into latitude and longitude coordinates and a Place ID, or converts\nlatitude and longitude coordinates or a Place ID into an address. The following\nuses combine well with data-driven styling for boundaries:\n\n- Use Geocoding to get the viewport for a region.\n- Apply component filtering to your Geocoding call to get the place IDs for administrative areas 1-4, locality, or postal code.\n- Use reverse geocoding to find place IDs by latitude and longitude coordinates, or even return place IDs for all components in a particular location.\n\nThe following example uses an address (url-escaped) to make a request to the\nGeocoding API: \n\n```html\nhttps://maps.googleapis.com/maps/api/geocode/json?address=1600%20Amphitheatre%20Pkwy%20Mountain%20View%20CA&key=YOUR_API_KEY\n```\n\nYou can use [reverse geocoding](/maps/documentation/geocoding/requests-reverse-geocoding)\nto find place IDs. The following example Geocoding\nservice function returns the place IDs for all address components at the\nspecified latitude and longitude coordinates: \n\n```html\nhttps://maps.googleapis.com/maps/api/geocode/json?latlng=41.864182,-87.676930&key=YOUR_API_KEY\n```\n\nUse reverse geocoding with component filtering to get the address component\nfor one or more of the following types at the specified location:\n\n- `administrativeArea`\n- `country`\n- `locality`\n- `postalCode`\n\nThe next example function shows using the Geocoding service, adding component\nrestrictions with reverse geocoding to get all of the address components at the\nspecified location for only the `locality` type: \n\n```html\nhttps://maps.googleapis.com/maps/api/geocode/json?latlng=41.864182,-87.676930&result_type=locality&key=YOUR_API_KEY\n```"]]