我正试图通过谷歌位置在谷歌地图上实现搜索。在我的xml中,我使用AutoCompleteTextView
元素。但当我点击search bar
时,谷歌搜索就会出现为什么?有可能在没有adapter
的情况下用我的searchbar
实现google places吗?
3秒视频屏幕
来自文档:
要将AutocompleteSupportFragment添加到您的应用程序中,请执行以下步骤:
- 将片段添加到活动的XML布局中
- 在你的活动或片段中添加一个监听器
布局:
<fragment android:id="@+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
/>
代码:
// Initialize the AutocompleteSupportFragment.
val autocompleteFragment =
supportFragmentManager.findFragmentById(R.id.autocomplete_fragment)
as AutocompleteSupportFragment
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME))
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: ${place.name}, ${place.id}")
}
override fun onError(status: Status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: $status")
}
})