use of com.google.android.libraries.places.api.model.AutocompletePrediction in project android-places-demos by googlemaps.
the class StringUtil method stringify.
static String stringify(FindAutocompletePredictionsResponse response, boolean raw) {
StringBuilder builder = new StringBuilder();
builder.append(response.getAutocompletePredictions().size()).append(" Autocomplete Predictions Results:");
if (raw) {
builder.append(RESULT_SEPARATOR);
appendListToStringBuilder(builder, response.getAutocompletePredictions());
} else {
for (AutocompletePrediction autocompletePrediction : response.getAutocompletePredictions()) {
builder.append(RESULT_SEPARATOR).append(autocompletePrediction.getFullText(/* matchStyle */
null));
}
}
return builder.toString();
}
use of com.google.android.libraries.places.api.model.AutocompletePrediction 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