Android Place Autocomplete Fragment自行关闭

时间:2016-12-03 14:27:46

标签: android android-fragments autocomplete

我正在尝试第一次实现地方自动完成片段并面临一个问题。当我开始输入时,在第一个字母后它开始搜索而不是显示结果它(片段)刚刚消失.. 我得知的是  状态{statusCode = PLACES_API_ACCESS_NOT_CONFIGURED,resolution = null}

的Manifest.xml

cellForRowAt

Xml_layout

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "CustomCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as! CustomCell
    if(selectedIndexPath != nil) {
        if(indexPath == selectedIndexPath) {
            cell.button.isHidden = false
        }
    }
    return cell
}

  <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4" />

On click fragment appears

fragment disappears/closed on its own

7 个答案:

答案 0 :(得分:23)

在Android上使用Google API进行开发时,这是一次非常有趣的体验。您需要做的就是:

  1. 转到console.developers.google.com
  2. 选择与Google API密钥相关联的项目
  3. 为Android启用Google地方API
  4. 就是这样。

答案 1 :(得分:4)

rand_mem_RAM是对的。
花了我2天的谷歌搜索,可能绕过了答案因为它太简单了。

  1. 转到console.developers.google.com
  2. 选择与Google API密钥相关联的项目
  3. 为Android启用Google Places API 3A。单击网页顶部的FIRST BLUE BUTTON,打开下一个网页 3B。接下来,单击下一个网页顶部的SECOND BLUE BUTTON 然后测试你的设备。
  4. 本来可以投票但不能作为嘉宾这样做。

答案 2 :(得分:2)

我也遇到了同样的问题,然后我偶然发现了此消息enter image description here,如果您想自己检查,这里是链接。 Places SDK for Android
长话短说,我迁移到了新的APIS,其说明在此处。 Migrating to the new Places SDK Client

在我的情况下我的地图在Fragment上,搜索在地图顶部

build.gradle

    implementation "com.google.android.gms:play-services-location:16.0.0"
    implementation "com.google.android.gms:play-services-maps:16.1.0"
    implementation 'com.google.android.libraries.places:places:1.0.0'

请注意场所API是最新的1.0.0。 这些API给了我:

用于MAP的
com.google.android.gms.maps.SupportMapFragment
用于SEARCH的
com.google.android.libraries.places.widget.AutocompleteSupportFragment 酒吧

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <fragment
        android:id="@+id/f_auto_complete"
        android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:minHeight="@dimen/size_search_view"
        app:layout_constraintEnd_toEndOf="@+id/map"
        app:layout_constraintStart_toStartOf="@id/map"
        app:layout_constraintTop_toTopOf="@id/map" />

片段

private fun initAutoCompleteView() {
        activity?.let { a ->
            if (!Places.isInitialized()) {
                Places.initialize(a.applicationContext, "YOUR API KEY")
            }
            fragment = childFragmentManager.findFragmentById(R.id.f_auto_complete) as AutocompleteSupportFragment
            fragment?.let {                    
                it.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG))
                it.setOnPlaceSelectedListener(object : PlaceSelectionListener {
                    override fun onPlaceSelected(place: Place) {
                        Log.i("Places", place.name)

                        }
                    }

                    override fun onError(status: Status) {
                        Log.i("Places", "An error occurred: $status")
                    }
                })
            }
        }
    }

为了使生活更轻松,我还添加了导入,以防万一所有类都不适合。

import com.google.android.libraries.places.api.Places
import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.widget.AutocompleteSupportFragment
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener

答案 3 :(得分:1)

另一个可能的原因是因为您尚未为项目启用计费。我就是这种情况。我尝试了此页面中的所有解决方案,但是在键入字符后,place-autocomplete-fragment仍然自行关闭。

因此,我从字面上回到基础知识,然后转到Places SDK for Android Getting Started page,它在该页面上提到在使用Android版Places SDK之前我们需要启用计费功能。那为我解决了问题。

要启用计费,请转到the billing page

答案 4 :(得分:0)

由于SHA1指纹可能会出现问题,在我的情况下,在更换调试sha1键后释放sha1 app工作正常

答案 5 :(得分:0)

我认为您应该禁用对api密钥的限制。这可能不是最安全的方法,但对我有用。

答案 6 :(得分:0)

如果您已根据限制添加了api,请在允许的API中添加place api

相关问题