Search in sources :

Example 1 with FetchPlaceRequest

use of com.google.android.libraries.places.api.net.FetchPlaceRequest 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 2 with FetchPlaceRequest

use of com.google.android.libraries.places.api.net.FetchPlaceRequest in project iNaturalistAndroid by inaturalist.

the class LocationChooserActivity method getPlaceDetails.

private void getPlaceDetails(AutocompletePrediction prediction, int index, CountDownLatch latch) {
    new Thread(() -> {
        if (prediction.getPlaceId() == null) {
            // We only show predictions with place IDs (so we can retrieve exact lat/lng)
            latch.countDown();
            return;
        }
        List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.VIEWPORT);
        FetchPlaceRequest request = FetchPlaceRequest.builder(prediction.getPlaceId(), fields).build();
        mPlacesClient.fetchPlace(request).addOnSuccessListener(fetchPlaceResponse -> {
            // Only when successfully fetching the place details -> add it to the results list
            Place googlePlace = fetchPlaceResponse.getPlace();
            LatLngBounds viewport = googlePlace.getViewport();
            Double radius = null;
            Double latitude = googlePlace.getLatLng().latitude;
            Double longitude = googlePlace.getLatLng().longitude;
            if (viewport != null) {
                LatLng center = viewport.getCenter();
                LatLng northeast = viewport.northeast;
                // Radius is the largest distance from geom center to one of the bounds corners
                radius = Math.max(distanceInMeters(latitude, longitude, center.latitude, center.longitude), distanceInMeters(latitude, longitude, northeast.latitude, northeast.longitude));
            } else {
                radius = 10.0;
            }
            if (index < mPlaces.size()) {
                mPlaces.get(index).accuracy = radius;
                mPlaces.get(index).latitude = latitude;
                mPlaces.get(index).longitude = longitude;
            }
            latch.countDown();
        }).addOnFailureListener(e -> {
            latch.countDown();
        });
    }).start();
}
Also used : Address(android.location.Address) TypeFilter(com.google.android.libraries.places.api.model.TypeFilter) CameraUpdateFactory(com.google.android.gms.maps.CameraUpdateFactory) AutocompleteSessionToken(com.google.android.libraries.places.api.model.AutocompleteSessionToken) Arrays(java.util.Arrays) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) LocationListener(android.location.LocationListener) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) VisibleRegion(com.google.android.gms.maps.model.VisibleRegion) ActionBar(androidx.appcompat.app.ActionBar) Manifest(android.Manifest) Locale(java.util.Locale) Handler(android.os.Handler) View(android.view.View) Animation(android.view.animation.Animation) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) ConnectivityManager(android.net.ConnectivityManager) LatLng(com.google.android.gms.maps.model.LatLng) FindAutocompletePredictionsRequest(com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest) IntentFilter(android.content.IntentFilter) NetworkInfo(android.net.NetworkInfo) Place(com.google.android.libraries.places.api.model.Place) BroadcastReceiver(android.content.BroadcastReceiver) Geocoder(android.location.Geocoder) Logger(org.tinylog.Logger) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TextView(android.widget.TextView) ListView(android.widget.ListView) Location(android.location.Location) Marker(com.google.android.gms.maps.model.Marker) LocationManager(android.location.LocationManager) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Bridge(com.livefront.bridge.Bridge) HashMap(java.util.HashMap) Intent(android.content.Intent) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Editable(android.text.Editable) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) PermissionChecker(androidx.core.content.PermissionChecker) AnimationUtils(android.view.animation.AnimationUtils) SuppressLint(android.annotation.SuppressLint) MenuInflater(android.view.MenuInflater) MotionEvent(android.view.MotionEvent) Toast(android.widget.Toast) Menu(android.view.Menu) State(com.evernote.android.state.State) SupportMapFragment(com.google.android.gms.maps.SupportMapFragment) DialogInterface(android.content.DialogInterface) MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) Places(com.google.android.libraries.places.api.Places) IOException(java.io.IOException) Point(android.graphics.Point) RectangularBounds(com.google.android.libraries.places.api.model.RectangularBounds) Spinner(android.widget.Spinner) TimeUnit(java.util.concurrent.TimeUnit) Color(android.graphics.Color) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) Configuration(android.content.res.Configuration) AutocompletePrediction(com.google.android.libraries.places.api.model.AutocompletePrediction) GoogleMap(com.google.android.gms.maps.GoogleMap) EditText(android.widget.EditText) OnClickListener(android.view.View.OnClickListener) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) List(java.util.List) ArrayList(java.util.ArrayList) LatLng(com.google.android.gms.maps.model.LatLng) Place(com.google.android.libraries.places.api.model.Place)

Example 3 with FetchPlaceRequest

use of com.google.android.libraries.places.api.net.FetchPlaceRequest in project hypertrack-live-android by hypertrack.

the class SearchPlacePresenter method selectItem.

public void selectItem(PlaceModel placeModel) {
    view.showProgressBar();
    if (!placeModel.isRecent) {
        state.addPlaceToRecent(placeModel);
    }
    List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS);
    FetchPlaceRequest request = FetchPlaceRequest.builder(placeModel.placeId, fields).setSessionToken(token).build();
    placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {

        @Override
        public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
            view.hideProgressBar();
            PlaceModel destination = new PlaceModel();
            destination.address = fetchPlaceResponse.getPlace().getAddress();
            destination.latLng = fetchPlaceResponse.getPlace().getLatLng();
            state.setDestination(destination);
            providePlace(destination);
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            view.hideProgressBar();
            if (e instanceof ApiException) {
                ApiException apiException = (ApiException) e;
                Log.e(TAG, "Place not found: " + apiException.getStatusCode());
            }
        }
    });
}
Also used : FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) PlaceModel(com.hypertrack.live.models.PlaceModel) FetchPlaceResponse(com.google.android.libraries.places.api.net.FetchPlaceResponse) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ApiException(com.google.android.gms.common.api.ApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 4 with FetchPlaceRequest

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

the class PlaceAndPhotoTestActivity method fetchPlace.

/**
 * Fetches the {@link Place} specified via the UI and displays it. May also trigger {@link
 * #fetchPhoto(PhotoMetadata)} if set in the UI.
 */
private void fetchPlace() {
    responseView.setText(null);
    photoView.setImageBitmap(null);
    dismissKeyboard(findViewById(R.id.place_id_field));
    final boolean isFetchPhotoChecked = isFetchPhotoChecked();
    List<Field> placeFields = getPlaceFields();
    String customPhotoReference = getCustomPhotoReference();
    if (!validateInputs(isFetchPhotoChecked, placeFields, customPhotoReference)) {
        return;
    }
    setLoading(true);
    FetchPlaceRequest request = FetchPlaceRequest.newInstance(getPlaceId(), placeFields);
    Task<FetchPlaceResponse> placeTask = placesClient.fetchPlace(request);
    placeTask.addOnSuccessListener((response) -> {
        responseView.setText(StringUtil.stringify(response, isDisplayRawResultsChecked()));
        if (isFetchPhotoChecked) {
            attemptFetchPhoto(response.getPlace());
        }
    });
    placeTask.addOnFailureListener((exception) -> {
        exception.printStackTrace();
        responseView.setText(exception.getMessage());
    });
    placeTask.addOnCompleteListener(response -> setLoading(false));
}
Also used : Field(com.google.android.libraries.places.api.model.Place.Field) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) FetchPlaceResponse(com.google.android.libraries.places.api.net.FetchPlaceResponse)

Example 5 with FetchPlaceRequest

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

the class PlacePhotosActivity method getPlacePhoto.

private void getPlacePhoto() {
    // [START maps_places_get_place_photos]
    // Define a Place ID.
    final String placeId = "INSERT_PLACE_ID_HERE";
    // Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
    final List<Place.Field> fields = Collections.singletonList(Place.Field.PHOTO_METADATAS);
    // Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
    final FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields);
    placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
        final Place place = response.getPlace();
        // Get the photo metadata.
        final List<PhotoMetadata> metadata = place.getPhotoMetadatas();
        if (metadata == null || metadata.isEmpty()) {
            Log.w(TAG, "No photo metadata.");
            return;
        }
        final PhotoMetadata photoMetadata = metadata.get(0);
        // Get the attribution text.
        final String attributions = photoMetadata.getAttributions();
        // Create a FetchPhotoRequest.
        final FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata).setMaxWidth(// Optional.
        500).setMaxHeight(// Optional.
        300).build();
        placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
            Bitmap bitmap = fetchPhotoResponse.getBitmap();
            imageView.setImageBitmap(bitmap);
        }).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_photos]
}
Also used : PhotoMetadata(com.google.android.libraries.places.api.model.PhotoMetadata) List(java.util.List) Bitmap(android.graphics.Bitmap) ImageView(android.widget.ImageView) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) Place(com.google.android.libraries.places.api.model.Place) PhotoMetadata(com.google.android.libraries.places.api.model.PhotoMetadata) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) ApiException(com.google.android.gms.common.api.ApiException) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Collections(java.util.Collections) Log(android.util.Log) FetchPhotoRequest(com.google.android.libraries.places.api.net.FetchPhotoRequest) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Bitmap(android.graphics.Bitmap) FetchPhotoRequest(com.google.android.libraries.places.api.net.FetchPhotoRequest) Place(com.google.android.libraries.places.api.model.Place) ApiException(com.google.android.gms.common.api.ApiException)

Aggregations

FetchPlaceRequest (com.google.android.libraries.places.api.net.FetchPlaceRequest)6 Log (android.util.Log)4 Place (com.google.android.libraries.places.api.model.Place)4 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)4 List (java.util.List)4 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)3 LatLng (com.google.android.gms.maps.model.LatLng)3 FetchPlaceResponse (com.google.android.libraries.places.api.net.FetchPlaceResponse)3 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 ApiException (com.google.android.gms.common.api.ApiException)2 Arrays (java.util.Arrays)2 Manifest (android.Manifest)1 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 SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1