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]
}
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]
}
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);
}
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]
}
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;
}
Aggregations