Search in sources :

Example 1 with FindAutocompletePredictionsResponse

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());
                    }
                }
            });
        }
    }
}
Also used : FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) FindAutocompletePredictionsResponse(com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ApiException(com.google.android.gms.common.api.ApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 2 with FindAutocompletePredictionsResponse

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()));
    });
}
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

FindAutocompletePredictionsRequest (com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)2 FindAutocompletePredictionsResponse (com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse)2 ReactMethod (com.facebook.react.bridge.ReactMethod)1 ReadableMap (com.facebook.react.bridge.ReadableMap)1 WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 ApiException (com.google.android.gms.common.api.ApiException)1 LatLng (com.google.android.gms.maps.model.LatLng)1 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1 AutocompletePrediction (com.google.android.libraries.places.api.model.AutocompletePrediction)1 Place (com.google.android.libraries.places.api.model.Place)1 ArrayList (java.util.ArrayList)1