使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
“地点搜索”组件
Places 界面套件的“地点搜索”组件会在列表中呈现地点搜索结果。
您可以自定义地点搜索列表。您可以指定:
- 要显示的内容
- 垂直方向的媒体尺寸
- 文本截断
- 方向
- 与您的品牌和应用的设计语言相匹配的主题替换项
- 注明内容来源的位置
- 相应地点是否可供选择
您还可以自定义请求,以执行 Search by text request
或 Search Nearby request
。
结算
每次更改 configureFromSearchByTextRequest()
或 configureFromSearchNearbyRequest()
绑定值时,系统都会向您收取费用。
向您的应用添加地点搜索功能
通过将 PlaceSearchFragment
fragment 添加到布局来使用地点搜索 widget。
当您希望应用加载文字搜索或附近搜索结果时,请使用相应请求调用 configureFromSearchByTextRequest()
或 configureFromSearchNearbyRequest()
。
Kotlin
fragment.configureFromSearchByTextRequest(searchByTextRequest)
// or fragment.configureFromSearchNearbyRequest(searchNearbyRequest) for nearby search
Java
fragment.configureFromSearchByTextRequest(searchByTextRequest)
// or fragment.configureFromSearchNearbyRequest(searchNearbyRequest) for nearby search
您还可以向组件添加可选的 PlaceSearchFragmentListener
,以便在组件加载时、选择地点时(如果设置为可选择)或加载组件时发生错误时接收回调。
Kotlin
fragment.registerListener(
object : PlaceSearchFragmentListener {
override fun onLoad(places: List<Place>) {...}
override fun onRequestError(e: Exception) {...}
override fun onPlaceSelected(place: Place) {...}
}
)
Java
fragment.registerListener(
new PlaceSearchFragmentListener() {
@Override public void onLoad(List<? extends Place> places) {...}
@Override public void onRequestError(Exception e) {...}
@Override public void onPlaceSelected(Place place) {...}
}
)
自定义地点搜索组件
自定义内容
您必须指定组件将显示哪些内容。
此示例将组件配置为显示 Place 的地址和评分。
Kotlin
val fragment = PlaceSearchFragment.newInstance(listOf(Content.ADDRESS, Content.RATING))
Java
PlaceSearchFragment fragment = PlaceSearchFragment.newInstance(listOf(Content.ADDRESS,Content.RATING));
您还可以选择自定义地点搜索组件中显示的内容的以下方面:
将自定义配置添加到 PlaceSearchFragment
。
Kotlin
fragment.preferTruncation = false
fragment.attributionPosition = AttributionPosition.BOTTOM
fragment.mediaSize = MediaSize.SMALL
fragment.selectable = true
Java
fragment.setPreferTruncation(false)
fragment.setAttributionPosition(AttributionPosition.BOTTOM)
fragment.setMediaSize(MediaSize.SMALL)
fragment.setSelectable(true)
自定义屏幕方向
默认方向为竖屏。对于横向,请在 PlaceSearchFragment.newInstance()
中指定 Orientation.HORIZONTAL
。
Kotlin
PlaceSearchFragment.newInstance(
PlaceSearchFragment.ALL_CONTENT,
Orientation.HORIZONTAL
)
Java
PlaceSearchFragment.newInstance(
PlaceSearchFragment.ALL_CONTENT,
Orientation.HORIZONTAL
)
自定义主题
实例化 fragment 时,您可以指定一个主题来替换任何默认样式属性。默认值为 PlacesMaterialTheme
。如需详细了解主题设置,请参阅 Place Details Component 文档。
未被覆盖的任何主题背景属性都将使用默认样式。如果您想支持深色主题,可以在 values-night/colors.xml
中为颜色添加条目。
<style name="CustomizedTheme" parent="PlacesMaterialTheme">
<item name="placesColorPrimary">@color/app_primary_color</item>
<item name="placesColorOnSurface">@color/app_color_on_surface</item>
<item name="placesColorOnSurfaceVariant">@color/app_color_on_surface</item>
<item name="placesTextAppearanceBodySmall">@style/app_text_appearence_small</item>
<item name="placesCornerRadius">20dp</item>
</style>
示例
Kotlin
val fragment: PlaceSearchFragment =
PlaceSearchFragment.newInstance(PlaceSearchFragment.STANDARD_CONTENT)
fragment.preferTruncation = false
fragment.attributionPosition = AttributionPosition.BOTTOM
fragment.mediaSize = MediaSize.SMALL
fragment.selectable = true
fragment.registerListener(
object : PlaceSearchFragmentListener {
override fun onLoad(places: List<Place>) {...}
override fun onRequestError(e: Exception) {...}
override fun onPlaceSelected(place: Place) {...}
}
)
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commitNow()
fragment.configureFromSearchByTextRequest(searchByTextRequest)
Java
PlaceSearchFragment fragment = PlaceSearchFragment.newInstance(PlaceSearchFragment.STANDARD_CONTENT);
fragment.setPreferTruncation(false)
fragment.setAttributionPosition(AttributionPosition.BOTTOM)
fragment.setMediaSize(MediaSize.SMALL)
fragment.setSelectable(true)
fragment.registerListener(
new PlaceSearchFragmentListener() {
@Override public void onLoad(List<? extends Place> places) {...}
@Override public void onRequestError(Exception e) {...}
@Override public void onPlaceSelected(Place place) {...}
}
)
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commitNow();
fragment.configureFromSearchByTextRequest(searchByTextRequest)
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-30。
[null,null,["最后更新时间 (UTC):2025-08-30。"],[],[],null,["Select platform: [Android](/maps/documentation/places/android-sdk/place-search-ui-kit \"View this page for the Android platform docs.\") [iOS](/maps/documentation/places/ios-sdk/place-search-ui-kit \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/places-ui-kit/place-list \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nPlace Search component\n======================\n\n\nThe Place Search component of the Places UI Kit renders the results of a place search in a list.\n\n\nYou can customize the Place Search list. You can specify:\n\n- The content to display\n- Media size in vertical orientation\n- Text truncation\n- The orientation\n- Theme overrides that match your brand and app's design language\n- The position of the attribution\n- Whether a place is selectable\n\n\nYou can also customize the request to perform either a [`Search by text request`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/api/net/SearchByTextRequest) or a [`Search Nearby request`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/api/net/SearchNearbyRequest).\n\nBilling\n-------\n\n\nYou are billed each time the `configureFromSearchByTextRequest()` or `configureFromSearchNearbyRequest()` binding value is changed.\n\nAdd Place Search to your app\n----------------------------\n\n| **Tip:** [See the complete example](/maps/documentation/places/android-sdk/place-search-ui-kit#example).\n\n\nUse the Place Search widget by adding the [`PlaceSearchFragment`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/PlaceSearchFragment#newInstance(kotlin.collections.List,com.google.android.libraries.places.widget.model.Orientation,kotlin.Int)) [Fragment](https://developer.android.com/guide/fragments) to your layout.\n\n\nWhen you want your app to load a text search or nearby search result, call `configureFromSearchByTextRequest()` or `configureFromSearchNearbyRequest()` with the request. \n\n### Kotlin\n\n```kotlin\nfragment.configureFromSearchByTextRequest(searchByTextRequest)\n\n// or fragment.configureFromSearchNearbyRequest(searchNearbyRequest) for nearby search\n```\n\n### Java\n\n```java\nfragment.configureFromSearchByTextRequest(searchByTextRequest)\n\n// or fragment.configureFromSearchNearbyRequest(searchNearbyRequest) for nearby search\n \n```\n\n\nYou can also add an optional `PlaceSearchFragmentListener` to the component to receive callbacks when the component loads, a place is selected (if set to be selectable) or when there is an error loading the component. \n\n### Kotlin\n\n```kotlin\nfragment.registerListener(\n object : PlaceSearchFragmentListener {\n override fun onLoad(places: List\u003cPlace\u003e) {...}\n override fun onRequestError(e: Exception) {...}\n override fun onPlaceSelected(place: Place) {...}\n }\n)\n \n```\n\n### Java\n\n```java\nfragment.registerListener(\n new PlaceSearchFragmentListener() {\n @Override public void onLoad(List\u003c? extends Place\u003e places) {...}\n @Override public void onRequestError(Exception e) {...}\n @Override public void onPlaceSelected(Place place) {...}\n }\n)\n \n```\n\nCustomize the Place Search component\n------------------------------------\n\n### Customize content\n\nYou must specify which content your component will display. **Note:** The Place name will always appear.\n\n\nThis example configures the component to display the address and rating of the Place. \n\n### Kotlin\n\n```kotlin\nval fragment = PlaceSearchFragment.newInstance(listOf(Content.ADDRESS, Content.RATING))\n \n```\n\n### Java\n\n```java\nPlaceSearchFragment fragment = PlaceSearchFragment.newInstance(listOf(Content.ADDRESS,Content.RATING));\n \n```\n\n\nYou can also optionally customize the following aspects of the content that appears in your Place Search component:\n\n- [PlaceSearchFragment.Content](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/PlaceSearchFragment.Content): The content shown in the component.\n- [mediaSize](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/model/MediaSize): The photo size in the vertical orientation of the fragment. The default is `SMALL`.\n- [preferTruncation](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/PlaceSearchFragment#setPreferTruncation(boolean)): Whether to truncate text of each Place Details view.\n- [attributionPosition](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/model/AttributionPosition): Whether to show the Google Maps attribution at the top or bottom of the component.\n- [selectable](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/PlaceSearchFragment#getSelectable()): Whether each place in the list is selectable.\n\n\nAdd your customization configuration to [`PlaceSearchFragment`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/PlaceSearchFragment). \n\n### Kotlin\n\n```kotlin\nfragment.preferTruncation = false\nfragment.attributionPosition = AttributionPosition.BOTTOM\nfragment.mediaSize = MediaSize.SMALL\nfragment.selectable = true\n \n```\n\n### Java\n\n```java\nfragment.setPreferTruncation(false)\nfragment.setAttributionPosition(AttributionPosition.BOTTOM)\nfragment.setMediaSize(MediaSize.SMALL)\nfragment.setSelectable(true)\n \n```\n\n### Customize orientation\n\n\nThe default orientation is vertical. For horizontal orientation, specify [`Orientation.HORIZONTAL`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/model/Orientation) in [`PlaceSearchFragment.newInstance()`](/maps/documentation/places/android-sdk/reference/com/google/android/libraries/places/widget/PlaceSearchFragment#newInstance(kotlin.collections.List,com.google.android.libraries.places.widget.model.Orientation,kotlin.Int)). \n\n### Kotlin\n\n```kotlin\nPlaceSearchFragment.newInstance(\n PlaceSearchFragment.ALL_CONTENT,\n Orientation.HORIZONTAL\n)\n \n```\n\n### Java\n\n```java\nPlaceSearchFragment.newInstance(\n PlaceSearchFragment.ALL_CONTENT,\n Orientation.HORIZONTAL\n)\n \n```\n\n### Customize the theme\n\n\nWhen instantiating a fragment, you can specify a theme that overrides any of the default style attributes. The default is `PlacesMaterialTheme`. See the [Place Details Component documentation](/maps/documentation/places/android-sdk/place-details-ui-kit#customize-place-details) for more information on theming.\n\n\nAny theme attributes that are not overridden use the default styles. If you'd like to support a dark theme, you can add an entry for the color in `values-night/colors.xml`. \n\n```transact-sql\n \u003cstyle name=\"CustomizedTheme\" parent=\"PlacesMaterialTheme\"\u003e\n \u003citem name=\"placesColorPrimary\"\u003e@color/app_primary_color\u003c/item\u003e\n \u003citem name=\"placesColorOnSurface\"\u003e@color/app_color_on_surface\u003c/item\u003e\n \u003citem name=\"placesColorOnSurfaceVariant\"\u003e@color/app_color_on_surface\u003c/item\u003e\n \n \u003citem name=\"placesTextAppearanceBodySmall\"\u003e@style/app_text_appearence_small\u003c/item\u003e\n \n \u003citem name=\"placesCornerRadius\"\u003e20dp\u003c/item\u003e\n \u003c/style\u003e\n```\n\nExample\n-------\n\n### Kotlin\n\n```kotlin\nval fragment: PlaceSearchFragment =\nPlaceSearchFragment.newInstance(PlaceSearchFragment.STANDARD_CONTENT)\n\nfragment.preferTruncation = false\nfragment.attributionPosition = AttributionPosition.BOTTOM\nfragment.mediaSize = MediaSize.SMALL\nfragment.selectable = true\n\nfragment.registerListener(\n object : PlaceSearchFragmentListener {\n override fun onLoad(places: List\u003cPlace\u003e) {...}\n override fun onRequestError(e: Exception) {...}\n override fun onPlaceSelected(place: Place) {...}\n }\n)\n\nsupportFragmentManager\n .beginTransaction()\n .replace(R.id.fragment_container, fragment)\n .commitNow()\n\nfragment.configureFromSearchByTextRequest(searchByTextRequest)\n \n```\n\n### Java\n\n```java\nPlaceSearchFragment fragment = PlaceSearchFragment.newInstance(PlaceSearchFragment.STANDARD_CONTENT);\n\nfragment.setPreferTruncation(false)\nfragment.setAttributionPosition(AttributionPosition.BOTTOM)\nfragment.setMediaSize(MediaSize.SMALL)\nfragment.setSelectable(true)\n\nfragment.registerListener(\n new PlaceSearchFragmentListener() {\n @Override public void onLoad(List\u003c? extends Place\u003e places) {...}\n @Override public void onRequestError(Exception e) {...}\n @Override public void onPlaceSelected(Place place) {...}\n }\n)\n\ngetSupportFragmentManager()\n .beginTransaction()\n .replace(R.id.fragment_container, fragment)\n .commitNow();\n\nfragment.configureFromSearchByTextRequest(searchByTextRequest)\n \n```"]]