Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Place Sdk #25

Open
ArisChoice opened this issue May 29, 2019 · 3 comments
Open

Update Place Sdk #25

ArisChoice opened this issue May 29, 2019 · 3 comments

Comments

@ArisChoice
Copy link

It will be great if you update this library as per this https://developers.google.com/places/android-sdk/client-migration
and thanks for this library

@saneera
Copy link

saneera commented Jun 5, 2019

any progress of this

@ArisChoice
Copy link
Author

@saneera updated SuggestionProvider .class like this -

`package com.alium.nibo.repo.location;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alium.nibo.R;
import com.alium.nibo.autocompletesearchbar.NiboSearchSuggestionItem;
import com.alium.nibo.repo.contracts.ISuggestionRepository;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.AutocompletePrediction;
import com.google.android.libraries.places.api.model.AutocompleteSessionToken;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.TypeFilter;
import com.google.android.libraries.places.api.net.FetchPlaceRequest;
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest;
import com.google.android.libraries.places.api.net.PlacesClient;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**

  • Created by abdulmujibaliu on 9/8/17.
    */

public class SuggestionsProvider implements ISuggestionRepository {

private String TAG = getClass().getSimpleName();
//    private GoogleApiClient mGoogleApiClient;
private Context mContext;

public SuggestionsProvider(GoogleApiClient mGoogleApiClient, Context mContext) {

// this.mGoogleApiClient = mGoogleApiClient;
this.mContext = mContext;
}

public Observable<Collection<NiboSearchSuggestionItem>> getSuggestions(final String query) {
    final List<NiboSearchSuggestionItem> placeSuggestionItems = new ArrayList<>();

    /*if (mGoogleApiClient == null) {
        Log.d(TAG, "Google play services cannot be null");
    }*/

    return new Observable<Collection<NiboSearchSuggestionItem>>() {
        @Override
        protected void subscribeActual(final Observer<? super Collection<NiboSearchSuggestionItem>> observer) {

// String query1 = query + "&key" + "AIzaSyD-_Cesk5Zw9HWBkqbJbNKUAaWd9yxdxb0";
Log.d(TAG, "getAutocompletePredictions +++++ " + query);

            AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
            FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
                    // Call either setLocationBias() OR setLocationRestriction().

// .setLocationBias(bounds)
//.setLocationRestriction(bounds)
.setCountry("IN")
// .setTypeFilter(TypeFilter.GEOCODE)
// .setTypeFilter(TypeFilter.ADDRESS)
// .setTypeFilter(TypeFilter.REGIONS)
.setSessionToken(token)
.setQuery(query)
.build();
PlacesClient placesClient = Places.createClient(mContext);
placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
placeSuggestionItems.clear();
for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
Log.i(TAG, prediction.getPlaceId());
Log.i(TAG, prediction.getPrimaryText(null).toString());
Log.d(TAG, prediction.getFullText(null).toString());
//Add as a new item to avoid IllegalArgumentsException when buffer is released
NiboSearchSuggestionItem placeSuggestion = new NiboSearchSuggestionItem(
prediction.getFullText(null).toString(),
prediction.getPlaceId(), NiboSearchSuggestionItem.TYPE_SEARCH_ITEM_SUGGESTION,
mContext.getResources().getDrawable(R.drawable.ic_map_marker_def)

                    );
                    placeSuggestionItems.add(placeSuggestion);
                }
                observer.onNext(placeSuggestionItems);
            }).addOnFailureListener((exception) -> {
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    Log.e(TAG, "Place not found: " + apiException.getStatusCode());
                    observer.onError(new Throwable("Place not found:"));
                }
            });






            /*Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query, null, null)
                    .setResultCallback(
                            new ResultCallback<AutocompletePrediction>() {
                                @Override
                                public void onResult(@NonNull AutocompletePrediction buffer) {
                                    placeSuggestionItems.clear();
                                    if (buffer.getStatus().isSuccess()) {
                                        Log.d(TAG, buffer.toString() + " " + buffer.getCount());

                                        for (AutocompletePrediction prediction : buffer) {
                                            Log.d(TAG, prediction.getFullText(null).toString());
                                            //Add as a new item to avoid IllegalArgumentsException when buffer is released
                                            NiboSearchSuggestionItem placeSuggestion = new NiboSearchSuggestionItem(
                                                    prediction.getFullText(null).toString(),
                                                    prediction.getPlaceId(), NiboSearchSuggestionItem.TYPE_SEARCH_ITEM_SUGGESTION,
                                                    mContext.getResources().getDrawable(R.drawable.ic_map_marker_def)
                                            );

                                            placeSuggestionItems.add(placeSuggestion);
                                        }

                                        observer.onNext(placeSuggestionItems);

                                    } else {
                                        Log.d(TAG, buffer.toString());
                                        observer.onError(new Throwable(buffer.getStatus().getStatusMessage()));
                                    }
                                    //Prevent memory leak by releasing buffer
                                    buffer.release();
                                }
                            }, 60, TimeUnit.SECONDS);*/

        }
    };
}


public Observable<Place> getPlaceByID(final String placeId) {
    return new Observable<Place>() {
        @Override
        protected void subscribeActual(final Observer<? super Place> observer) {
            PlacesClient placesClient = Places.createClient(mContext);

            // Specify the fields to return (in this example all fields are returned).
            List<Place.Field> placeFields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.ID, Place.Field.NAME);

// Construct a request object, passing the place ID and fields array.
FetchPlaceRequest request = FetchPlaceRequest.builder(placeId, placeFields).build();

            placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
                Place place = response.getPlace();
                Log.i(TAG, "Place found: " + place.getName());
                LatLng queriedLocation = place.getLatLng();
                Log.i(TAG, "PlaceLAT long: " + queriedLocation);
                observer.onNext(place);
            }).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());
                    observer.onError(new Throwable("Place not found:"));
                }
            });














            /*Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
                    .setResultCallback(new ResultCallback<PlaceBuffer>() {
                        @Override
                        public void onResult(@NonNull PlaceBuffer places) {
                            if (places.getStatus().isSuccess()) {
                                final Place thatPlace = places.get(0);
                                LatLng queriedLocation = thatPlace.getLatLng();
                                Log.v("Latitude is", "" + queriedLocation.latitude);
                                Log.v("Longitude is", "" + queriedLocation.longitude);

                                observer.onNext(thatPlace.freeze());
                            } else {

                            }
                            places.release();
                        }
                    });*/
        }
    }.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

@Override
public void stop() {
    mContext = null;

// mGoogleApiClient = null;
}

}
`

@saneera
Copy link

saneera commented Jun 18, 2019

Thanks, it worked perfectly

I need to another favour from you

I need to add placekicker origin and destination with my own map like uber design,
can you able to send me the design for that simple design

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants