Bạn có thể sử dụng Dịch vụ địa điểm và Geocoding API với Maps SDK cho Android để tìm kiếm các khu vực và nhận thêm thông tin về các địa điểm. Places Service và Geocoding API là những lựa chọn thay thế mạnh mẽ và ổn định để lấy mã địa điểm. Nếu đã sử dụng mã địa điểm, bạn có thể dùng lại các mã đó với kiểu dựa trên dữ liệu cho ranh giới.
Thêm Dịch vụ Địa điểm và Mã hoá địa lý vào Maps SDK cho các ứng dụng Android theo những cách sau:
Places SDK for Android là một thư viện Android chứa các phương thức trả về thông tin về các địa điểm.
Places API trả về thông tin về các địa điểm bằng cách sử dụng yêu cầu HTTP.
Lớp Geocoder có thể mã hoá địa lý và mã hoá địa lý ngược một cách linh hoạt từ thông tin đầu vào của người dùng.
Geocoding API cho phép bạn mã hoá địa lý các địa chỉ tĩnh đã biết.
Sử dụng Dịch vụ địa điểm
Sử dụng tính năng Tìm kiếm bằng văn bản (Mới) để tìm mã địa điểm
Bạn có thể sử dụng Text Search (New) REST API trong Places API để nhận mã địa điểm bao gồm dữ liệu khu vực bằng cách chỉ định places.id trong mặt nạ trường. Bạn sẽ không mất phí khi sử dụng tính năng Tìm kiếm văn bản (Mới) để chỉ yêu cầu mã địa điểm. Tìm hiểu thêm.
Ví dụ: để lấy mã địa điểm cho Trinidad, CA, bạn có thể thực hiện lệnh gọi API sau:
Sử dụng tính năng Place Autocomplete để tìm các khu vực
Dịch vụ Place Autocomplete trong Places SDK cho Android cung cấp một cách thuận tiện để cho phép người dùng tìm kiếm các khu vực. Để định cấu hình dịch vụ Places Autocomplete chỉ trả về các khu vực, hãy dùng AutocompleteSupportFragment.setTypesFilter(List) để đặt bộ lọc loại thành PlaceTypes.REGIONS.
Nhận thông tin chi tiết về địa điểm cho một khu vực
Dịch vụ Thông tin chi tiết về địa điểm trong Places SDK dành cho Android trả về dữ liệu cho một khu vực có thể khá hữu ích. Ví dụ như bạn có thể:
Tìm mã địa điểm ranh giới dựa trên tên địa điểm.
Lấy khung nhìn để thu phóng đến một ranh giới.
Lấy loại đối tượng cho ranh giới (ví dụ: locality).
Nhận địa chỉ được định dạng, phân giải thành "Tên địa điểm, Tiểu bang, Quốc gia" ở khu vực Hoa Kỳ (ví dụ: "Ottumwa, IA, Hoa Kỳ").
Nhận các dữ liệu hữu ích khác, chẳng hạn như ảnh.
Sử dụng Geocoding API
Geocoding API cho phép bạn chuyển đổi một địa chỉ thành toạ độ vĩ độ và kinh độ cũng như mã địa điểm, hoặc chuyển đổi toạ độ vĩ độ và kinh độ hoặc mã địa điểm thành địa chỉ. Các trường hợp sử dụng sau đây kết hợp tốt với kiểu dựa trên dữ liệu cho ranh giới:
Sử dụng tính năng Mã hoá địa lý để lấy khung hiển thị cho một khu vực.
Áp dụng bộ lọc thành phần cho lệnh gọi Địa lý mã hoá để nhận mã địa điểm cho các khu vực hành chính 1-4, địa phương hoặc mã bưu chính.
Sử dụng tính năng địa lý mã hoá ngược để tìm mã địa điểm theo toạ độ (vĩ độ và kinh độ), hoặc thậm chí trả về mã địa điểm cho tất cả các thành phần ở một vị trí cụ thể.
Ví dụ sau đây sử dụng một địa chỉ (đã thoát URL) để gửi yêu cầu đến Geocoding API:
Bạn có thể sử dụng tính năng mã hoá địa lý ngược để tìm mã địa điểm. Ví dụ sau đây về hàm dịch vụ Mã hoá địa lý sẽ trả về mã địa điểm cho tất cả các thành phần địa chỉ tại toạ độ vĩ độ và kinh độ đã chỉ định:
Sử dụng tính năng chuyển đổi địa chỉ thành toạ độ địa lý ngược có bộ lọc thành phần để lấy thành phần địa chỉ cho một hoặc nhiều loại sau đây tại vị trí đã chỉ định:
administrativeArea
country
locality
postalCode
Hàm ví dụ tiếp theo cho thấy cách sử dụng dịch vụ Mã hoá địa lý, thêm các hạn chế về thành phần bằng tính năng mã hoá địa lý ngược để nhận tất cả các thành phần địa chỉ tại vị trí được chỉ định chỉ cho loại locality:
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 UTC."],[[["\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```"]]