透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Place Search 元件
Places UI Kit 的 Place Search 元件會以清單形式顯示地點搜尋結果。
您可以自訂地點搜尋清單。您可以指定:
- 要顯示的內容
- 直向模式的媒體大小
- 文字截斷
- 方向
- 符合品牌和應用程式設計語言的主題覆寫
- 註明內容來源的位置
- 地點是否可選取
您也可以自訂要求,執行 Search by text request
或 Search Nearby request
。
帳單
每當 configureFromSearchByTextRequest()
或 configureFromSearchNearbyRequest()
繫結值變更時,系統就會向您收費。
在應用程式中加入地點搜尋功能
將 PlaceSearchFragment
Fragment 新增至版面配置,即可使用「地點搜尋」小工具。
如要讓應用程式載入文字搜尋或附近搜尋結果,請使用要求呼叫 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 Search 元件
自訂內容
您必須指定元件要顯示的內容。
這個範例會設定元件,顯示地點的地址和評分。
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
)
自訂主題
例項化片段時,您可以指定主題來覆寫任何預設樣式屬性。預設值為 PlacesMaterialTheme
。如要進一步瞭解主題設定,請參閱地點詳細資料元件說明文件。
未覆寫的主題屬性會使用預設樣式。如要支援深色主題,可以在 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)
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-30 (世界標準時間)。
[null,null,["上次更新時間: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```"]]