Search in sources :

Example 1 with Place

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

the class PlaceAutocompleteActivity method programmaticPlacePredictions.

// [END maps_places_on_activity_result]
private void programmaticPlacePredictions(String query) {
    // [START maps_places_programmatic_place_predictions]
    // 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 fetchPlace()).
    AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
    // Create a RectangularBounds object.
    RectangularBounds bounds = RectangularBounds.newInstance(new LatLng(-33.880490, 151.184363), new LatLng(-33.858754, 151.229596));
    // Use the builder to create a FindAutocompletePredictionsRequest.
    FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder().setLocationBias(bounds).setOrigin(new LatLng(-33.8749937, 151.2041382)).setCountries("AU", "NZ").setTypeFilter(TypeFilter.ADDRESS).setSessionToken(token).setQuery(query).build();
    placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
        for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
            Log.i(TAG, prediction.getPlaceId());
            Log.i(TAG, prediction.getPrimaryText(null).toString());
        }
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + apiException.getStatusCode());
        }
    });
// [END maps_places_programmatic_place_predictions]
}
Also used : TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) AutocompleteActivity(com.google.android.libraries.places.widget.AutocompleteActivity) Arrays(java.util.Arrays) Bundle(android.os.Bundle) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) Intent(android.content.Intent) PlaceSelectionListener(com.google.android.libraries.places.widget.listener.PlaceSelectionListener) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) ArrayList(java.util.ArrayList) Log(android.util.Log) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) Places(com.google.android.libraries.places.api.Places) AutocompleteSupportFragment(com.google.android.libraries.places.widget.AutocompleteSupportFragment) Place(com.google.android.libraries.places.api.model.Place) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) List(java.util.List) Nullable(androidx.annotation.Nullable) Autocomplete(com.google.android.libraries.places.widget.Autocomplete) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) Status(com.google.android.gms.common.api.Status) NotNull(org.jetbrains.annotations.NotNull) ApiException(com.google.android.gms.common.api.ApiException) AutocompleteActivityMode(com.google.android.libraries.places.widget.model.AutocompleteActivityMode) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) LatLng(com.google.android.gms.maps.model.LatLng) ApiException(com.google.android.gms.common.api.ApiException)

Example 2 with Place

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

the class PlaceAutocompleteActivity method initAutocompleteSupportFragment.

private void initAutocompleteSupportFragment() {
    // [START maps_places_autocomplete_support_fragment]
    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    // Specify the types of place data to return.
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
    // Set up a PlaceSelectionListener to handle the response.
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

        @Override
        public void onPlaceSelected(@NotNull Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        }

        @Override
        public void onError(@NotNull Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });
    // [END maps_places_autocomplete_support_fragment]
    // [START maps_places_autocomplete_location_bias]
    autocompleteFragment.setLocationBias(RectangularBounds.newInstance(new LatLng(-33.880490, 151.184363), new LatLng(-33.858754, 151.229596)));
    // [END maps_places_autocomplete_location_bias]
    // [START maps_places_autocomplete_location_restriction]
    autocompleteFragment.setLocationRestriction(RectangularBounds.newInstance(new LatLng(-33.880490, 151.184363), new LatLng(-33.858754, 151.229596)));
    // [END maps_places_autocomplete_location_restriction]
    // [START maps_places_autocomplete_type_filter]
    autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);
    // [END maps_places_autocomplete_type_filter]
    List<Place.Field> fields = new ArrayList<>();
    // [START maps_places_intent_type_filter]
    Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).setTypeFilter(TypeFilter.ADDRESS).build(this);
    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
    // [END maps_places_intent_type_filter]
    // [START maps_places_autocomplete_country_filter]
    autocompleteFragment.setCountries("AU", "NZ");
// [END maps_places_autocomplete_country_filter]
}
Also used : Status(com.google.android.gms.common.api.Status) ArrayList(java.util.ArrayList) AutocompleteSupportFragment(com.google.android.libraries.places.widget.AutocompleteSupportFragment) Intent(android.content.Intent) LatLng(com.google.android.gms.maps.model.LatLng) PlaceSelectionListener(com.google.android.libraries.places.widget.listener.PlaceSelectionListener) Place(com.google.android.libraries.places.api.model.Place)

Example 3 with Place

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

the class AutocompleteTestActivity method onActivityResult.

/**
 * Called when AutocompleteActivity finishes
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == AutocompleteActivity.RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(intent);
            responseView.setText(StringUtil.stringifyAutocompleteWidget(place, isDisplayRawResultsChecked()));
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            Status status = Autocomplete.getStatusFromIntent(intent);
            responseView.setText(status.getStatusMessage());
        } else if (resultCode == AutocompleteActivity.RESULT_CANCELED) {
        // The user canceled the operation.
        }
    }
    // Required because this class extends AppCompatActivity which extends FragmentActivity
    // which implements this method to pass onActivityResult calls to child fragments
    // (eg AutocompleteFragment).
    super.onActivityResult(requestCode, resultCode, intent);
}
Also used : Status(com.google.android.gms.common.api.Status) Place(com.google.android.libraries.places.api.model.Place)

Example 4 with Place

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

the class PlaceDetailsActivity method getPlaceById.

private void getPlaceById() {
    // [START maps_places_get_place_by_id]
    // Define a Place ID.
    final String placeId = "INSERT_PLACE_ID_HERE";
    // Specify the fields to return.
    final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
    // Construct a request object, passing the place ID and fields array.
    final FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
    placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
        Place place = response.getPlace();
        Log.i(TAG, "Place found: " + place.getName());
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            final ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + exception.getMessage());
            final int statusCode = apiException.getStatusCode();
        // TODO: Handle error with given status code.
        }
    });
// [END maps_places_get_place_by_id]
}
Also used : Arrays(java.util.Arrays) List(java.util.List) LatLng(com.google.android.gms.maps.model.LatLng) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) Place(com.google.android.libraries.places.api.model.Place) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) ApiException(com.google.android.gms.common.api.ApiException) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Log(android.util.Log) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Place(com.google.android.libraries.places.api.model.Place) ApiException(com.google.android.gms.common.api.ApiException)

Example 5 with Place

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

the class RNGooglePlacesModule method propertiesMapForPlace.

private WritableMap propertiesMapForPlace(Place place, List<Place.Field> selectedFields) {
    // Display attributions if required.
    // CharSequence attributions = place.getAttributions();
    WritableMap map = Arguments.createMap();
    if (selectedFields.contains(Place.Field.LAT_LNG)) {
        WritableMap locationMap = Arguments.createMap();
        locationMap.putDouble("latitude", place.getLatLng().latitude);
        locationMap.putDouble("longitude", place.getLatLng().longitude);
        map.putMap("location", locationMap);
    }
    if (selectedFields.contains(Place.Field.NAME)) {
        map.putString("name", place.getName());
    }
    if (selectedFields.contains(Place.Field.ADDRESS)) {
        if (!TextUtils.isEmpty(place.getAddress())) {
            map.putString("address", place.getAddress());
        } else {
            map.putString("address", "");
        }
    }
    if (selectedFields.contains(Place.Field.ADDRESS_COMPONENTS)) {
        if (place.getAddressComponents() != null) {
            List<AddressComponent> items = place.getAddressComponents().asList();
            WritableNativeArray addressComponents = new WritableNativeArray();
            for (AddressComponent item : items) {
                WritableMap addressComponentMap = Arguments.createMap();
                addressComponentMap.putArray("types", Arguments.fromList(item.getTypes()));
                addressComponentMap.putString("name", item.getName());
                addressComponentMap.putString("shortName", item.getShortName());
                addressComponents.pushMap(addressComponentMap);
            }
            map.putArray("addressComponents", addressComponents);
        } else {
            WritableArray emptyResult = Arguments.createArray();
            map.putArray("addressComponents", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.PHONE_NUMBER)) {
        if (!TextUtils.isEmpty(place.getPhoneNumber())) {
            map.putString("phoneNumber", place.getPhoneNumber());
        } else {
            map.putString("phoneNumber", "");
        }
    }
    if (selectedFields.contains(Place.Field.WEBSITE_URI)) {
        if (null != place.getWebsiteUri()) {
            map.putString("website", place.getWebsiteUri().toString());
        } else {
            map.putString("website", "");
        }
    }
    if (selectedFields.contains(Place.Field.ID)) {
        map.putString("placeID", place.getId());
    }
    if (place.getAttributions() != null) {
        List<String> attributions = new ArrayList<>(place.getAttributions());
        map.putArray("attributions", Arguments.fromArray(attributions.toArray(new String[0])));
    } else {
        WritableArray emptyResult = Arguments.createArray();
        map.putArray("attributions", emptyResult);
    }
    if (selectedFields.contains(Place.Field.TYPES)) {
        if (place.getTypes() != null) {
            List<String> types = new ArrayList<>();
            for (Place.Type placeType : place.getTypes()) {
                types.add(RNGooglePlacesPlaceTypeMapper.getTypeSlug(placeType));
            }
            map.putArray("types", Arguments.fromArray(types.toArray(new String[0])));
        } else {
            WritableArray emptyResult = Arguments.createArray();
            map.putArray("types", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.VIEWPORT)) {
        if (place.getViewport() != null) {
            WritableMap viewportMap = Arguments.createMap();
            viewportMap.putDouble("latitudeNE", place.getViewport().northeast.latitude);
            viewportMap.putDouble("longitudeNE", place.getViewport().northeast.longitude);
            viewportMap.putDouble("latitudeSW", place.getViewport().southwest.latitude);
            viewportMap.putDouble("longitudeSW", place.getViewport().southwest.longitude);
            map.putMap("viewport", viewportMap);
        } else {
            WritableMap emptyResult = Arguments.createMap();
            map.putMap("viewport", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.PRICE_LEVEL)) {
        if (place.getPriceLevel() != null) {
            map.putInt("priceLevel", place.getPriceLevel());
        } else {
            map.putInt("priceLevel", 0);
        }
    }
    if (selectedFields.contains(Place.Field.RATING)) {
        if (place.getRating() != null) {
            map.putDouble("rating", place.getRating());
        } else {
            map.putDouble("rating", 0);
        }
    }
    if (selectedFields.contains(Place.Field.OPENING_HOURS)) {
        if (place.getOpeningHours() != null) {
            List<String> openingHours = new ArrayList<>(place.getOpeningHours().getWeekdayText());
            map.putArray("openingHours", Arguments.fromArray(openingHours.toArray(new String[0])));
        } else {
            WritableArray emptyResult = Arguments.createArray();
            map.putArray("openingHours", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.PLUS_CODE)) {
        if (place.getPlusCode() != null) {
            WritableMap plusCodeMap = Arguments.createMap();
            plusCodeMap.putString("compoundCode", place.getPlusCode().getCompoundCode());
            plusCodeMap.putString("globalCode", place.getPlusCode().getGlobalCode());
            map.putMap("plusCode", plusCodeMap);
        } else {
            WritableMap emptyResult = Arguments.createMap();
            map.putMap("plusCode", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.USER_RATINGS_TOTAL)) {
        if (place.getUserRatingsTotal() != null) {
            map.putInt("userRatingsTotal", place.getUserRatingsTotal());
        } else {
            map.putInt("userRatingsTotal", 0);
        }
    }
    return map;
}
Also used : WritableMap(com.facebook.react.bridge.WritableMap) AddressComponent(com.google.android.libraries.places.api.model.AddressComponent) WritableArray(com.facebook.react.bridge.WritableArray) ArrayList(java.util.ArrayList) WritableNativeArray(com.facebook.react.bridge.WritableNativeArray) Place(com.google.android.libraries.places.api.model.Place)

Aggregations

Place (com.google.android.libraries.places.api.model.Place)13 Status (com.google.android.gms.common.api.Status)8 Bundle (android.os.Bundle)6 Log (android.util.Log)6 LatLng (com.google.android.gms.maps.model.LatLng)6 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Intent (android.content.Intent)5 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)5 FetchPlaceRequest (com.google.android.libraries.places.api.net.FetchPlaceRequest)5 AutocompleteSupportFragment (com.google.android.libraries.places.widget.AutocompleteSupportFragment)5 PlaceSelectionListener (com.google.android.libraries.places.widget.listener.PlaceSelectionListener)5 Arrays (java.util.Arrays)5 Places (com.google.android.libraries.places.api.Places)4 AutocompletePrediction (com.google.android.libraries.places.api.model.AutocompletePrediction)4 AutocompleteSessionToken (com.google.android.libraries.places.api.model.AutocompleteSessionToken)4 RectangularBounds (com.google.android.libraries.places.api.model.RectangularBounds)4 TypeFilter (com.google.android.libraries.places.api.model.TypeFilter)4 FindAutocompletePredictionsRequest (com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest)4