Search in sources :

Example 1 with Places

use of com.google.android.libraries.places.api.Places in project android-places-demos by googlemaps.

the class ProgrammaticAutocompleteToolbarActivity method getPlacePredictions.

/**
 * This method demonstrates the programmatic approach to getting place predictions. The
 * parameters in this request are currently biased to Kolkata, India.
 *
 * @param query the plus code query string (e.g. "GCG2+3M K")
 */
private void getPlacePredictions(String query) {
    // The value of 'bias' biases prediction results to the rectangular region provided
    // (currently Kolkata). Modify these values to get results for another area. Make sure to
    // pass in the appropriate value/s for .setCountries() in the
    // FindAutocompletePredictionsRequest.Builder object as well.
    final LocationBias bias = RectangularBounds.newInstance(// SW lat, lng
    new LatLng(22.458744, 88.208162), // NE lat, lng
    new LatLng(22.730671, 88.524896));
    // Create a new programmatic Place Autocomplete request in Places SDK for Android
    final FindAutocompletePredictionsRequest newRequest = FindAutocompletePredictionsRequest.builder().setSessionToken(sessionToken).setLocationBias(bias).setTypeFilter(TypeFilter.ESTABLISHMENT).setQuery(query).setCountries("IN").build();
    // Perform autocomplete predictions request
    placesClient.findAutocompletePredictions(newRequest).addOnSuccessListener((response) -> {
        List<AutocompletePrediction> predictions = response.getAutocompletePredictions();
        adapter.setPredictions(predictions);
        progressBar.setIndeterminate(false);
        viewAnimator.setDisplayedChild(predictions.isEmpty() ? 0 : 1);
    }).addOnFailureListener((exception) -> {
        progressBar.setIndeterminate(false);
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + apiException.getStatusCode());
        }
    });
}
Also used : DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) Volley(com.android.volley.toolbox.Volley) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) GeocodingResult(com.example.placesdemo.model.GeocodingResult) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) GsonBuilder(com.google.gson.GsonBuilder) MenuItem(android.view.MenuItem) JSONException(org.json.JSONException) R(com.example.placesdemo.R) Gson(com.google.gson.Gson) Handler(android.os.Handler) Menu(android.view.Menu) RecyclerView(androidx.recyclerview.widget.RecyclerView) Log(android.util.Log) LocationBias(com.google.android.libraries.places.api.model.LocationBias) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Method(com.android.volley.Request.Method) SearchView(android.widget.SearchView) RequestQueue(com.android.volley.RequestQueue) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) MainActivity(com.example.placesdemo.MainActivity) LatLng(com.google.android.libraries.maps.model.LatLng) Places(com.google.android.libraries.places.api.Places) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) List(java.util.List) Nullable(androidx.annotation.Nullable) BuildConfig(com.example.placesdemo.BuildConfig) ViewAnimator(android.widget.ViewAnimator) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) OnQueryTextListener(android.widget.SearchView.OnQueryTextListener) ApiException(com.google.android.gms.common.api.ApiException) JSONArray(org.json.JSONArray) LocationBias(com.google.android.libraries.places.api.model.LocationBias) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) List(java.util.List) LatLng(com.google.android.libraries.maps.model.LatLng) ApiException(com.google.android.gms.common.api.ApiException)

Example 2 with Places

use of com.google.android.libraries.places.api.Places in project react-native-google-places by tolu360.

the class RNGooglePlacesModule method lookUpPlaceByID.

@ReactMethod
public void lookUpPlaceByID(String placeID, ReadableArray fields, 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;
    }
    List<Place.Field> selectedFields = getPlaceFields(fields.toArrayList(), false);
    FetchPlaceRequest request = FetchPlaceRequest.builder(placeID, selectedFields).build();
    placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
        Place place = response.getPlace();
        WritableMap map = propertiesMapForPlace(place, selectedFields);
        promise.resolve(map);
    }).addOnFailureListener((exception) -> {
        promise.reject("E_PLACE_DETAILS_ERROR", new Error(exception.getMessage()));
    });
}
Also used : TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) Arrays(java.util.Arrays) Bundle(android.os.Bundle) FindAutocompletePredictionsResponse(com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse) PackageManager(android.content.pm.PackageManager) PlaceLikelihood(com.google.android.libraries.places.api.model.PlaceLikelihood) ACCESS_FINE_LOCATION(android.Manifest.permission.ACCESS_FINE_LOCATION) FindCurrentPlaceRequest(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest) PlaceSelectionListener(com.google.android.libraries.places.widget.listener.PlaceSelectionListener) FindCurrentPlaceResponse(com.google.android.libraries.places.api.net.FindCurrentPlaceResponse) WritableNativeArray(com.facebook.react.bridge.WritableNativeArray) FetchPlaceResponse(com.google.android.libraries.places.api.net.FetchPlaceResponse) Log(android.util.Log) LocationBias(com.google.android.libraries.places.api.model.LocationBias) ReadableMap(com.facebook.react.bridge.ReadableMap) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) ContextCompat(android.support.v4.content.ContextCompat) AutocompleteSupportFragment(com.google.android.libraries.places.widget.AutocompleteSupportFragment) Place(com.google.android.libraries.places.api.model.Place) ActivityCompat(android.support.v4.app.ActivityCompat) RequiresPermission(android.support.annotation.RequiresPermission) Objects(java.util.Objects) List(java.util.List) ReactMethod(com.facebook.react.bridge.ReactMethod) Arguments(com.facebook.react.bridge.Arguments) Nullable(android.support.annotation.Nullable) AutocompleteActivityMode(com.google.android.libraries.places.widget.model.AutocompleteActivityMode) LocationRestriction(com.google.android.libraries.places.api.model.LocationRestriction) AutocompleteActivity(com.google.android.libraries.places.widget.AutocompleteActivity) WritableArray(com.facebook.react.bridge.WritableArray) ACCESS_WIFI_STATE(android.Manifest.permission.ACCESS_WIFI_STATE) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) ReadableArray(com.facebook.react.bridge.ReadableArray) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Intent(android.content.Intent) ActivityEventListener(com.facebook.react.bridge.ActivityEventListener) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Task(com.google.android.gms.tasks.Task) ArrayList(java.util.ArrayList) Promise(com.facebook.react.bridge.Promise) IntentSender(android.content.IntentSender) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) ReactProp(com.facebook.react.uimanager.annotations.ReactProp) ReactContextBaseJavaModule(com.facebook.react.bridge.ReactContextBaseJavaModule) Places(com.google.android.libraries.places.api.Places) TextUtils(android.text.TextUtils) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) TimeUnit(java.util.concurrent.TimeUnit) AddressComponent(com.google.android.libraries.places.api.model.AddressComponent) Autocomplete(com.google.android.libraries.places.widget.Autocomplete) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) Manifest.permission(android.Manifest.permission) WritableMap(com.facebook.react.bridge.WritableMap) Status(com.google.android.gms.common.api.Status) Activity(android.app.Activity) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) WritableMap(com.facebook.react.bridge.WritableMap) Place(com.google.android.libraries.places.api.model.Place) ReactMethod(com.facebook.react.bridge.ReactMethod)

Aggregations

Bundle (android.os.Bundle)2 Log (android.util.Log)2 Places (com.google.android.libraries.places.api.Places)2 AutocompletePrediction (com.google.android.libraries.places.api.model.AutocompletePrediction)2 AutocompleteSessionToken (com.google.android.libraries.places.api.model.AutocompleteSessionToken)2 LocationBias (com.google.android.libraries.places.api.model.LocationBias)2 RectangularBounds (com.google.android.libraries.places.api.model.RectangularBounds)2 TypeFilter (com.google.android.libraries.places.api.model.TypeFilter)2 FindAutocompletePredictionsRequest (com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)2 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)2 List (java.util.List)2 Manifest.permission (android.Manifest.permission)1 ACCESS_FINE_LOCATION (android.Manifest.permission.ACCESS_FINE_LOCATION)1 ACCESS_WIFI_STATE (android.Manifest.permission.ACCESS_WIFI_STATE)1 Activity (android.app.Activity)1 Intent (android.content.Intent)1 IntentSender (android.content.IntentSender)1 PackageManager (android.content.pm.PackageManager)1 Handler (android.os.Handler)1 Nullable (android.support.annotation.Nullable)1