我正在尝试通过谷歌地方在谷歌地图上实现搜索。但是谷歌搜索出现了



我正试图通过谷歌位置在谷歌地图上实现搜索。在我的xml中,我使用AutoCompleteTextView元素。但当我点击search bar时,谷歌搜索就会出现为什么?有可能在没有adapter的情况下用我的searchbar实现google places吗?

3秒视频屏幕

来自文档:

要将AutocompleteSupportFragment添加到您的应用程序中,请执行以下步骤:

  1. 将片段添加到活动的XML布局中
  2. 在你的活动或片段中添加一个监听器

布局

<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")
}
})

最新更新