将地图服务和 Geocoding API 与边界的数据驱动型样式搭配使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以将 Places 服务和 Geocoding API 与 Maps SDK for Android 搭配使用,以搜索区域并获取有关地点的更多信息。Places 服务和 Geocoding API 是强大且稳定的替代方案,可以用来获取地点 ID。如果您已经在使用地点 ID,就可以重复使用这些 ID 来设置边界的数据驱动型样式。
您可以通过以下方式将地点服务和地理编码服务添加到您的 Maps SDK for Android 应用:
使用 Places 服务
使用新版文本搜索功能查找地点 ID
您可以使用 Places API 中的新版文本搜索 REST API 来获取包含区域数据的地点 ID,只需在字段掩码中指定 places.id
即可。如果您仅使用新版文本搜索来请求地点 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 SDK for Android 中的地点自动补全服务让您的用户可以方便地搜索区域。如需将地点自动补全服务配置为仅返回区域,请使用 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
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):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```"]]