Android Place Autocomplete Fragment自行关闭

Far*_*lid 7 android autocomplete android-fragments

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

的Manifest.xml

  <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4" />
Run Code Online (Sandbox Code Playgroud)

Xml_layout

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="14dp"
    android:id="@+id/Area"
    android:onClick="Onselection"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true" />
<fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_below="@+id/Area"
        android:layout_alignParentStart="true" />
Run Code Online (Sandbox Code Playgroud)

public class test extends AppCompatActivity implements PlaceSelectionListener {


    private static final String LOG_TAG = "PlaceSelectionListener";
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
            new LatLng(-85, -180), new LatLng(85, 180));
    private static final int REQUEST_SELECT_PLACE = 1000;
    TextView locationtext ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testtttt);
        locationtext = (TextView) findViewById(R.id.Arear) ;

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

        autocompleteFragment.setOnPlaceSelectedListener(this);

        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .build();
        autocompleteFragment.setFilter(typeFilter);


    }


    public  void Onselection(View v)
    {
        try {
            Intent intent = new PlaceAutocomplete.IntentBuilder
                    (PlaceAutocomplete.MODE_OVERLAY)
                    .setBoundsBias(BOUNDS_MOUNTAIN_VIEW)
                    .build(testtttt.this);
            startActivityForResult(intent, REQUEST_SELECT_PLACE);
        } catch (GooglePlayServicesRepairableException |
                GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onPlaceSelected(Place place) {

    }

    @Override
    public void onError(Status status) {

    }
Run Code Online (Sandbox Code Playgroud)

出现点击片段

片段自行消失/关闭

小智 24

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

  1. 转到console.developers.google.com
  2. 选择与Google API密钥关联的项目
  3. 为Android启用google places API

而已.


小智 5

rand_mem_RAM是正确的。
花了我两天的谷歌搜索时间,可能绕过了答案,因为它太简单了。

  1. 转到console.developers.google.com
  2. 选择与Google API密钥关联的项目
  3. 启用适用于Android 3a的Google Places API。单击网页顶部的第一个蓝色按钮,这将打开下一个网页3b。接下来,单击下一个网页顶部的第二个蓝色按钮,然后测试您的设备。

本来可以投票,但作为客人却不能这样做。


Ran*_*ngh 5

我遇到了同样的问题,然后我偶然发现了这条消息 在此处输入图片说明如果你想自己检查,这里是链接。适用于 Android 的 Places SDK
长话短说,我迁移到了新的 APIS,这里给出了它的说明。迁移到新的 Places SDK 客户端

就我而言, 我的地图位于 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'
Run Code Online (Sandbox Code Playgroud)

请注意 API 是全新的 1.0.0。这些 API 给了我:
com.google.android.gms.maps.SupportMapFragment for MAP
com.google.android.libraries.places.widget.AutocompleteSupportFragment for SEARCH BAR

    <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" />
Run Code Online (Sandbox Code Playgroud)

分段

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")
                    }
                })
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

为了让生活更轻松,我还添加了导入,以防万一任何类不适合在一起。

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
Run Code Online (Sandbox Code Playgroud)