我正在使用自定义活动在Google Places API上进行自动完成请求。
我们在示例中使用相同的代码: 在我的适配器中:
// 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();
但是我要去的地方会得到一个地方的细节:
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());
}
});
我做到了:
request.getSessionToken();
我得到了null令牌,我认为这导致对API的许多查询。 在文档中说,有必要在调用 fetchPlace()时再次传递令牌,但是在哪里?
答案 0 :(得分:0)
您可以为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();
}
});