9

I am trying to implement place autocomplete fragment for the first time and facing one issue with it.When ever i starts to type, after first letter it starts searching and instead of showing the results its (fragment) just disappears.. insted that i am getting is Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null}

Manifest.xml

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

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" />

Class

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) {

    }

On click fragment appears

ScreenShot

fragment disappears/closed on its own

ScreenShot

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
Farrukh khalid
  • 93
  • 1
  • 2
  • 9
  • I leave this for future readers : https://iteritory.com/android-google-places-autocomplete-feature-using-new-places-sdk/?fbclid=IwAR3-nV3ELTilG_W79xmC-UhI9Gkag58Odykg0fW8THisBH7aNb46PCmAnz4 – Kwnstantinos Nikoloutsos Apr 05 '19 at 14:40

7 Answers7

24

This is a very intriguing experience while developing with Google APIs on Android. All that you need to do is:

  1. Go to console.developers.google.com
  2. Select your project associated with the Google API Key
  3. Enable google places API for android

That's it.

rand_mem_RAM
  • 586
  • 5
  • 6
5

I was facing the same issue, then I stumbled across this message enter image description here here is the link if you want to check for yourself. Places SDK for Android.
So long story short I migrated to new APIS, whose instructions are given here. Migrating to the new Places SDK Client

In my case My map is on Fragment and search was on top of map

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'

Notice the places APIs are fresh and new 1.0.0. These APIs gave me:
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" />

Fragment

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

To make life easy, I am adding imports as well just in case any of the classes don't fit together.

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
Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
4

rand_mem_RAM is right.
Took me 2days googling, probably bypassed the answer because it's sooo simple.

  1. Go to console.developers.google.com
  2. Select your project associated with the Google API Key
  3. Enable google places API for android 3a. Click FIRST BLUE BUTTON at top of web page, this opens next web page 3b. Next, click SECOND BLUE BUTTON at top of next web page Then test your device.

Would have upvoted but can't do so as a guest.

boxmoja
  • 49
  • 1
  • 1
2

Another possible cause is because you haven't enable billing for your project yet. That was the case with me; I tried all the solutions in this page but the place-autocomplete-fragment still keeps on closing by itself after I typed in a character.

So I literally went back to basics and went to Places SDK for Android Getting Started page and it mentioned on that page that we need to enable billing prior to using Places SDK for Android. That solves the problem for me.

To enable billing, go to the billing page.

imin
  • 4,504
  • 13
  • 56
  • 103
1

Sometime problem may be occur because of SHA1 fingerprint, in my case after replace debug sha1 key with release sha1 app working fine

Ashwin Nirmale
  • 443
  • 4
  • 13
0

I think you should disable restrictions on the api key. It's probably not the safest way but it worked for me.

0

If you have added the api as restricted, add the place api in the allowed API