Mateus Carvalho
Mateus Carvalho

Reputation: 336

Where set the Session token in fetchPlace() on Places API?

I`m using a custom Activity to make AutoComplete requests on Google Places API.

We use the same code on examples: In my Adapter:

  // Use the builder to create a FindAutocompletePredictionsRequest.
    FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
            // Call either setLocationBias() OR setLocationRestriction().
            // .setLocationBias(bounds)
            .setLocationBias(mBounds)
            .setCountry("br")
            //   .setTypeFilter(TypeFilter.ADDRESS)
            .setSessionToken(session)
            .setQuery(constraint.toString())
            .build();

But where I`ll go get the details of a place:

 List<Place.Field> placeFields = Arrays.asList(Place.Field.LAT_LNG);
        FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
        //request.getSessionToken(); TEST TOKEN

        placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
            Place place = response.getPlace();
            returnResult(new LatLng(place.getLatLng().latitude, place.getLatLng().longitude), mResultList.get(position).primaryText.toString());
            //Log.i(TAG, "Place found: " + place.getName());
        }).addOnFailureListener((exception) -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                int statusCode = apiException.getStatusCode();
                // Handle error with given status code.
                Log.e(TAG, "Place not found: " + exception.getMessage());
            }
        });

And I did:

request.getSessionToken();

I get the null token, I think that is causing many queries on API. In the documentation say that is necessary pass the token again when call fetchPlace() but where?

Upvotes: 3

Views: 2096

Answers (1)

gsanthosh91
gsanthosh91

Reputation: 371

You can set session token for FetchPlaceRequest

List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS);
        FetchPlaceRequest request = FetchPlaceRequest.builder(placeId, placeFields).setSessionToken(token).build();
        placesClient.fetchPlace(request).addOnSuccessListener(response -> {
            Log.d("LocationPickActvity", "AutocompleteSessionToken: "+request.getSessionToken());
            Place place = response.getPlace();
            clickListener.place(place);
        }).addOnFailureListener(exception -> {
            if (exception instanceof ApiException) {
                Toast.makeText(mContext, ""+exception.getMessage() + "", Toast.LENGTH_SHORT).show();
            }
        });

Upvotes: 4

Related Questions