Search in sources :

Example 6 with AutocompletePrediction

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();
}
Also used : AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction)

Example 7 with AutocompletePrediction

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()));
    });
}
Also used : WritableMap(com.facebook.react.bridge.WritableMap) WritableArray(com.facebook.react.bridge.WritableArray) ArrayList(java.util.ArrayList) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) ReadableMap(com.facebook.react.bridge.ReadableMap) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsResponse(com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse) Place(com.google.android.libraries.places.api.model.Place) ReactMethod(com.facebook.react.bridge.ReactMethod)

Aggregations

AutocompletePrediction (com.google.android.libraries.places.api.model.AutocompletePrediction)7 FindAutocompletePredictionsRequest (com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)4 Bundle (android.os.Bundle)3 Log (android.util.Log)3 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)3 LatLng (com.google.android.gms.maps.model.LatLng)3 Places (com.google.android.libraries.places.api.Places)3 AutocompleteSessionToken (com.google.android.libraries.places.api.model.AutocompleteSessionToken)3 Place (com.google.android.libraries.places.api.model.Place)3 RectangularBounds (com.google.android.libraries.places.api.model.RectangularBounds)3 TypeFilter (com.google.android.libraries.places.api.model.TypeFilter)3 Intent (android.content.Intent)2 Handler (android.os.Handler)2 Menu (android.view.Menu)2 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Manifest (android.Manifest)1 SuppressLint (android.annotation.SuppressLint)1