首页 > 解决方案 > 我正在尝试通过 Google Places 在 Google Maps 上实现搜索。但是谷歌搜索出现了

问题描述

我正在尝试通过 Google Places 在 Google Maps 上实现搜索。在我的 xml 中,我使用AutoCompleteTextView元素。但是当我点击search bar- 谷歌搜索出现为什么?是否可以在没有我的searchbar情况下实现 google 地方adapter

3秒视频画面

标签: androidgoogle-mapskotlinmaps

解决方案


来自文档

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

推荐阅读