Kalyan Ram Adiraju
Kalyan Ram Adiraju

Reputation: 1

Places api not returning expected results after migrating to new sdk

Places autocomplete suggestions not giving expected results after Migrating to the New Places SDK Client, Old implementation was giving many POIS , After migration its giving very less point of interest's

    if (mGoogleApiClient.isConnected()) {

        // Submit the query to the autocomplete API and retrieve a PendingResult that will
        // contain the results when the query completes.

        RectangularBounds rectangularBounds = RectangularBounds.newInstance(mBounds);

        final FindAutocompletePredictionsRequest.Builder requestBuilder =
                FindAutocompletePredictionsRequest.builder()
                        .setQuery(constraint.toString())
                        .setLocationBias(rectangularBounds)
                        .setSessionToken(AutocompleteSessionToken.newInstance())
                        .setTypeFilter(TypeFilter.ADDRESS);

        Task<FindAutocompletePredictionsResponse> results =
                mPlacesClient.findAutocompletePredictions(requestBuilder.build());
        //Wait to get results.
        try {
            Tasks.await(results, 60, TimeUnit.SECONDS);
        } catch (ExecutionException | InterruptedException | TimeoutException e) {
            e.printStackTrace();

        }

        if (results.isSuccessful()) {
            if (results.getResult() != null) {
                resultList = new ArrayList<>(results.getResult().getAutocompletePredictions().size());
                Iterator<AutocompletePrediction> iterator = results.getResult().getAutocompletePredictions().iterator();
                while (iterator.hasNext()) {
                    AutocompletePrediction prediction = iterator.next();
                    resultList.add(new PlaceAutocompleteClass(prediction.getPlaceId(), prediction.getPrimaryText(CHARACTER_STYLE),
                            prediction.getSecondaryText(CHARACTER_STYLE)));

                }

                return resultList;
            }
            return null;
        } else {
            return null;
        }


    }
    return null;

}

Upvotes: 0

Views: 398

Answers (2)

Nazmus Saadat
Nazmus Saadat

Reputation: 973

Remove .setTypeFilter(TypeFilter.ADDRESS) from your FindAutocompletePredictionsRequest builder. It works for me.

Upvotes: 3

Shivam Sharma
Shivam Sharma

Reputation: 96

Here is the code for new Google Autocomplete places API

protected val PLACE_AUTOCOMPLETE_REQUEST_CODE = 1

     private fun getPlaceFromGoogle() {
                // Set the fields to specify which types of place data to return.
                val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)
                // Start the autocomplete intent.
                val intent = activity?.let {
                    Autocomplete.IntentBuilder(
                            AutocompleteActivityMode.OVERLAY, fields)
                            .build(it)
                }
                startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE)
            }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
            when (resultCode) {
                Activity.RESULT_OK -> {
                    val place = data?.let { Autocomplete.getPlaceFromIntent(it) }
                    place.let {
                    Log.i(TAG,  place?.name)
                    }
                }
                PlaceAutocomplete.RESULT_ERROR -> {
                    val status = PlaceAutocomplete.getStatus(activity, data)
                    Log.i(TAG, status.statusMessage)
                }

        }
    }

Upvotes: 0

Related Questions