Автозаполнение не работает для карт Google. При попытке добавить название места в "поле поиска" - PullRequest
1 голос
/ 02 августа 2020

Я пытаюсь использовать автозаполнение для доступа к местам на карте googlemap. Но когда я нажимаю на «окно поиска» и набираю название места, он возвращается к основной активности вместо отображения списка связанных названий мест, которые я ввел .

Мой код MainActivity: --->

package com.example.realmemap;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.RectangularBounds;
import com.google.android.libraries.places.api.model.TypeFilter;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.libraries.places.widget.AutocompleteSupportFragment;
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener;

import java.lang.reflect.Array;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Places.initialize(getApplicationContext(),"AIzaSyBUplVE-keAaH_YGmrGOPtVAYfoQTNpG1E");
    PlacesClient placesClient = Places.createClient(this);

    AutocompleteSupportFragment autocompleteSupportFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    autocompleteSupportFragment.setTypeFilter(TypeFilter.ADDRESS);

    autocompleteSupportFragment.setLocationBias(RectangularBounds.newInstance(
            new LatLng(5,4),
            new LatLng(5.2,4.3)));
    autocompleteSupportFragment.setCountries("IN");

    autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID,Place.Field.NAME));

    autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place place) {
            Log.i("1","Place:" + place.getName() + ", " + place.getId());

        }

        @Override
        public void onError(@NonNull Status status) {
            Log.i("2","An error Occurred:" + status);

        }
    });

}
}

XML код: ---->

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
    android:id="@+id/autocomplete_fragment"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="68dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Build.Gradle (зависимость): ---->

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'com.google.android.libraries.places:places:2.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

Информация Logcat, когда я начинаю вводить в строке поиска: ----->

I/2: An error Occurred:Status{statusCode=This IP, site or mobile 
application 
is not authorized to use this API key. Request received from IP address 
157.35.244.133, with empty referer, resolution=null}
D/EGL_emulation: eglMakeCurrent: 0xee902a80: ver 2 0 (tinfo 0xeec7c230)
D/TransportRuntime.CctTransportBackend: Making request to: 
https://firebaselogging.googleapis.com/v0cc/log/batch
W/ample.realmema: Accessing hidden method Lsun/misc/Unsafe;- 
>getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, 
allowed)
 W/ample.realmema: Accessing hidden method Lsun/misc/Unsafe;- 
>copyMemory(JJJ)V 
(greylist,core-platform-api, linking, allowed)
Accessing hidden method Lsun/misc/Unsafe;->getByte(J)B (greylist,core- 
platform-api, linking, allowed)
Accessing hidden method Lsun/misc/Unsafe;->getByte(Ljava/lang/Object;J)B 
(greylist,core-platform-api, linking, allowed)
Accessing hidden method Lsun/misc/Unsafe;->getLong(J)J (greylist,core- 
platform-api, linking, allowed)
Accessing hidden method Lsun/misc/Unsafe;->putByte(JB)V (greylist,core- 
platform-api, linking, allowed)
W/ample.realmema: Accessing hidden method Lsun/misc/Unsafe;- 
>putByte(Ljava/lang/Object;JB)V (greylist,core-platform-api, linking, 
 allowed)
W/ample.realmema: Accessing hidden method Lsun/misc/Unsafe;- 
>getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, 
allowed)
W/ample.realmema: Accessing hidden method Lsun/misc/Unsafe;- 
>getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, 
allowed)
I/TransportRuntime.CctTransportBackend: Status Code: 200
Content-Type: text/plain; charset=UTF-8
Content-Encoding: gzip

Любая помощь приветствуется

Спасибо за чтение

...