Wade_09225
Wade_09225

Reputation: 31

Places.GeoDataApi.getAutocompletePredictions

I'm having problems with Android Google Places API - auto complete feature.

I'm trying to use Places.GeoDataApi.getAutocompletePredictions() method to get the placeId, however, it doesn't work.(The output doesn't even show "can't get the value") Could anyone help me?

Here is my Java code:

 PendingResult<AutocompletePredictionBuffer> result =
            Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query,
                    mLatLonBounds, null);

    result.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() {
        @Override
        public void onResult(@NonNull AutocompletePredictionBuffer autocompletePredictions) {

            if (autocompletePredictions.getStatus().isSuccess()) { //如果回傳SUCCESS

                Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
                resultList = new ArrayList<>(autocompletePredictions.getCount());

                while (iterator.hasNext()) {
                    AutocompletePrediction prediction = iterator.next();
                    resultList.add(prediction.getPlaceId().toString());
                }
            }
           else {
                resultList.add("can't get the vaule");
            }
            autocompletePredictions.release();
        }

    });

 textView.setText(resultList.get(0).toString());

Thanks.

Upvotes: 3

Views: 1629

Answers (1)

Don Bosco
Don Bosco

Reputation: 41

I had the same issue and I got it worked by following the below steps

  • Enable the Google Places API for Android in developers console and check on the credentials page that your key is still present.
  • Add the required library to your module (play-services-places:11.2.0+).
  • Use the below code to get the predictions

    Task<AutocompletePredictionBufferResponse> results = mGeoDataClient.getAutocompletePredictions(yourInputString, yourLatLngBounds, yourTypeFilter);
    
  • Add on success listener for the predictions as below

    results.addOnSuccessListener(new OnSuccessListener<AutocompletePredictionBufferResponse>() {
    
    @Override
    public void onSuccess(AutocompletePredictionBufferResponse autocompletePredictions) {
        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        suggesstions = new ArrayList<String>(autocompletePredictions.getCount());
        if (iterator.hasNext()) {
            while (iterator.hasNext()) {
                AutocompletePrediction prediction = iterator.next();
                //do your stuff with prediction here
            }
        }else {
          //Nothing matches...
        }
        autocompletePredictions.release();
    }
    });
    
  • Add on failure listener for the predictions as below

    results.addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {
              Toast.makeText(MapsActivity.this,"Error while fetching suggessions : "+e.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
      }
    
    });
    

    If all the above code snippet is placed properly, then you probably check your internet connectivity.

Hope the above code will be helpful.

Thanks.

Upvotes: 4

Related Questions