use of com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse in project hypertrack-live-android by hypertrack.
the class SearchPlacePresenter method search.
public void search(String query) {
if (!state.mapDestinationMode) {
if (TextUtils.isEmpty(query)) {
view.updateList(state.getRecentPlaces());
} else {
// Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
// and once again when the user makes a selection (for example when calling selectPlace()).
token = AutocompleteSessionToken.newInstance();
// Use the builder to create a FindAutocompletePredictionsRequest.
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder().setTypeFilter(TypeFilter.GEOCODE).setSessionToken(token).setQuery(query).build();
placesClient.findAutocompletePredictions(request).addOnSuccessListener(new OnSuccessListener<FindAutocompletePredictionsResponse>() {
@Override
public void onSuccess(FindAutocompletePredictionsResponse response) {
view.updateList(PlaceModel.from(response.getAutocompletePredictions()));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
}
});
}
}
}
use of com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse in project react-native-google-places by tolu360.
the class RNGooglePlacesModule method getAutocompletePredictions.
@ReactMethod
public void getAutocompletePredictions(String query, ReadableMap options, final Promise promise) {
this.pendingPromise = promise;
if (!Places.isInitialized()) {
promise.reject("E_API_KEY_ERROR", new Error("No API key defined in gradle.properties or errors initializing Places"));
return;
}
String type = options.getString("type");
String country = options.getString("country");
country = country.isEmpty() ? null : country;
boolean useSessionToken = options.getBoolean("useSessionToken");
ReadableMap locationBias = options.getMap("locationBias");
double biasToLatitudeSW = locationBias.getDouble("latitudeSW");
double biasToLongitudeSW = locationBias.getDouble("longitudeSW");
double biasToLatitudeNE = locationBias.getDouble("latitudeNE");
double biasToLongitudeNE = locationBias.getDouble("longitudeNE");
ReadableMap locationRestriction = options.getMap("locationRestriction");
double restrictToLatitudeSW = locationRestriction.getDouble("latitudeSW");
double restrictToLongitudeSW = locationRestriction.getDouble("longitudeSW");
double restrictToLatitudeNE = locationRestriction.getDouble("latitudeNE");
double restrictToLongitudeNE = locationRestriction.getDouble("longitudeNE");
FindAutocompletePredictionsRequest.Builder requestBuilder = FindAutocompletePredictionsRequest.builder().setQuery(query);
if (country != null) {
requestBuilder.setCountry(country);
}
if (biasToLatitudeSW != 0 && biasToLongitudeSW != 0 && biasToLatitudeNE != 0 && biasToLongitudeNE != 0) {
requestBuilder.setLocationBias(RectangularBounds.newInstance(new LatLng(biasToLatitudeSW, biasToLongitudeSW), new LatLng(biasToLatitudeNE, biasToLongitudeNE)));
}
if (restrictToLatitudeSW != 0 && restrictToLongitudeSW != 0 && restrictToLatitudeNE != 0 && restrictToLongitudeNE != 0) {
requestBuilder.setLocationRestriction(RectangularBounds.newInstance(new LatLng(restrictToLatitudeSW, restrictToLongitudeSW), new LatLng(restrictToLatitudeNE, restrictToLongitudeNE)));
}
requestBuilder.setTypeFilter(getFilterType(type));
if (useSessionToken) {
requestBuilder.setSessionToken(AutocompleteSessionToken.newInstance());
}
Task<FindAutocompletePredictionsResponse> task = placesClient.findAutocompletePredictions(requestBuilder.build());
task.addOnSuccessListener((response) -> {
if (response.getAutocompletePredictions().size() == 0) {
WritableArray emptyResult = Arguments.createArray();
promise.resolve(emptyResult);
return;
}
WritableArray predictionsList = Arguments.createArray();
for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
WritableMap map = Arguments.createMap();
map.putString("fullText", prediction.getFullText(null).toString());
map.putString("primaryText", prediction.getPrimaryText(null).toString());
map.putString("secondaryText", prediction.getSecondaryText(null).toString());
map.putString("placeID", prediction.getPlaceId().toString());
if (prediction.getPlaceTypes().size() > 0) {
List<String> types = new ArrayList<>();
for (Place.Type placeType : prediction.getPlaceTypes()) {
types.add(RNGooglePlacesPlaceTypeMapper.getTypeSlug(placeType));
}
map.putArray("types", Arguments.fromArray(types.toArray(new String[0])));
}
predictionsList.pushMap(map);
}
promise.resolve(predictionsList);
});
task.addOnFailureListener((exception) -> {
promise.reject("E_AUTOCOMPLETE_ERROR", new Error(exception.getMessage()));
});
}
Aggregations