Search in sources :

Example 1 with FindCurrentPlaceRequest

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

the class CurrentPlaceActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // [START maps_places_current_place]
    // Use fields to define the data types to return.
    List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME);
    // Use the builder to create a FindCurrentPlaceRequest.
    FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
    // Call findCurrentPlace and handle the response (first check that the user has granted permission).
    if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
        placeResponse.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                FindCurrentPlaceResponse response = task.getResult();
                for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                    Log.i(TAG, String.format("Place '%s' has likelihood: %f", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood()));
                }
            } else {
                Exception exception = task.getException();
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    Log.e(TAG, "Place not found: " + apiException.getStatusCode());
                }
            }
        });
    } else {
        // A local method to request required permissions;
        // See https://developer.android.com/training/permissions/requesting
        getLocationPermission();
    }
// [END maps_places_current_place]
}
Also used : FindCurrentPlaceRequest(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest) PlaceLikelihood(com.google.android.libraries.places.api.model.PlaceLikelihood) ApiException(com.google.android.gms.common.api.ApiException) FindCurrentPlaceResponse(com.google.android.libraries.places.api.net.FindCurrentPlaceResponse) ApiException(com.google.android.gms.common.api.ApiException)

Example 2 with FindCurrentPlaceRequest

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

the class CurrentPlaceTestActivity method findCurrentPlaceWithPermissions.

/**
 * Fetches a list of {@link PlaceLikelihood} instances that represent the Places the user is
 * most
 * likely to be at currently.
 */
@RequiresPermission(allOf = { ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE })
private void findCurrentPlaceWithPermissions() {
    setLoading(true);
    FindCurrentPlaceRequest currentPlaceRequest = FindCurrentPlaceRequest.newInstance(getPlaceFields());
    Task<FindCurrentPlaceResponse> currentPlaceTask = placesClient.findCurrentPlace(currentPlaceRequest);
    currentPlaceTask.addOnSuccessListener((response) -> responseView.setText(StringUtil.stringify(response, isDisplayRawResultsChecked())));
    currentPlaceTask.addOnFailureListener((exception) -> {
        exception.printStackTrace();
        responseView.setText(exception.getMessage());
    });
    currentPlaceTask.addOnCompleteListener(task -> setLoading(false));
}
Also used : FindCurrentPlaceRequest(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest) FindCurrentPlaceResponse(com.google.android.libraries.places.api.net.FindCurrentPlaceResponse) RequiresPermission(androidx.annotation.RequiresPermission)

Example 3 with FindCurrentPlaceRequest

use of com.google.android.libraries.places.api.net.FindCurrentPlaceRequest in project android-samples by googlemaps.

the class MapsActivityCurrentPlace method showCurrentPlace.

// [END maps_current_place_on_request_permissions_result]
/**
 * Prompts the user to select the current place from a list of likely places, and shows the
 * current place on the map - provided the user has granted location permission.
 */
// [START maps_current_place_show_current_place]
private void showCurrentPlace() {
    if (map == null) {
        return;
    }
    if (locationPermissionGranted) {
        // Use fields to define the data types to return.
        List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG);
        // Use the builder to create a FindCurrentPlaceRequest.
        FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
        // Get the likely places - that is, the businesses and other points of interest that
        // are the best match for the device's current location.
        @SuppressWarnings("MissingPermission") final Task<FindCurrentPlaceResponse> placeResult = placesClient.findCurrentPlace(request);
        placeResult.addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {

            @Override
            public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
                if (task.isSuccessful() && task.getResult() != null) {
                    FindCurrentPlaceResponse likelyPlaces = task.getResult();
                    // Set the count, handling cases where less than 5 entries are returned.
                    int count;
                    if (likelyPlaces.getPlaceLikelihoods().size() < M_MAX_ENTRIES) {
                        count = likelyPlaces.getPlaceLikelihoods().size();
                    } else {
                        count = M_MAX_ENTRIES;
                    }
                    int i = 0;
                    likelyPlaceNames = new String[count];
                    likelyPlaceAddresses = new String[count];
                    likelyPlaceAttributions = new List[count];
                    likelyPlaceLatLngs = new LatLng[count];
                    for (PlaceLikelihood placeLikelihood : likelyPlaces.getPlaceLikelihoods()) {
                        // Build a list of likely places to show the user.
                        likelyPlaceNames[i] = placeLikelihood.getPlace().getName();
                        likelyPlaceAddresses[i] = placeLikelihood.getPlace().getAddress();
                        likelyPlaceAttributions[i] = placeLikelihood.getPlace().getAttributions();
                        likelyPlaceLatLngs[i] = placeLikelihood.getPlace().getLatLng();
                        i++;
                        if (i > (count - 1)) {
                            break;
                        }
                    }
                    // Show a dialog offering the user the list of likely places, and add a
                    // marker at the selected place.
                    MapsActivityCurrentPlace.this.openPlacesDialog();
                } else {
                    Log.e(TAG, "Exception: %s", task.getException());
                }
            }
        });
    } else {
        // The user has not granted permission.
        Log.i(TAG, "The user did not grant location permission.");
        // Add a default marker, because the user hasn't selected a place.
        map.addMarker(new MarkerOptions().title(getString(R.string.default_info_title)).position(defaultLocation).snippet(getString(R.string.default_info_snippet)));
        // Prompt the user for permission.
        getLocationPermission();
    }
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) FindCurrentPlaceResponse(com.google.android.libraries.places.api.net.FindCurrentPlaceResponse) FindCurrentPlaceRequest(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest) List(java.util.List) LatLng(com.google.android.gms.maps.model.LatLng) PlaceLikelihood(com.google.android.libraries.places.api.model.PlaceLikelihood)

Example 4 with FindCurrentPlaceRequest

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

the class RNGooglePlacesModule method findCurrentPlaceWithPermissions.

/**
 * Fetches a list of {@link PlaceLikelihood} instances that represent the Places the user is
 * most
 * likely to be at currently.
 */
@RequiresPermission(allOf = { ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE })
private void findCurrentPlaceWithPermissions(List<Place.Field> selectedFields, final Promise promise) {
    FindCurrentPlaceRequest currentPlaceRequest = FindCurrentPlaceRequest.newInstance(selectedFields);
    Task<FindCurrentPlaceResponse> currentPlaceTask = placesClient.findCurrentPlace(currentPlaceRequest);
    currentPlaceTask.addOnSuccessListener((response) -> {
        if (response.getPlaceLikelihoods().size() == 0) {
            WritableArray emptyResult = Arguments.createArray();
            promise.resolve(emptyResult);
            return;
        }
        WritableArray likelyPlacesList = Arguments.createArray();
        for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
            WritableMap map = propertiesMapForPlace(placeLikelihood.getPlace(), selectedFields);
            map.putDouble("likelihood", placeLikelihood.getLikelihood());
            likelyPlacesList.pushMap(map);
        }
        promise.resolve(likelyPlacesList);
    });
    currentPlaceTask.addOnFailureListener((exception) -> {
        promise.reject("E_CURRENT_PLACE_ERROR", new Error(exception.getMessage()));
    });
}
Also used : WritableMap(com.facebook.react.bridge.WritableMap) FindCurrentPlaceRequest(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest) WritableArray(com.facebook.react.bridge.WritableArray) PlaceLikelihood(com.google.android.libraries.places.api.model.PlaceLikelihood) FindCurrentPlaceResponse(com.google.android.libraries.places.api.net.FindCurrentPlaceResponse) RequiresPermission(android.support.annotation.RequiresPermission)

Aggregations

FindCurrentPlaceRequest (com.google.android.libraries.places.api.net.FindCurrentPlaceRequest)4 FindCurrentPlaceResponse (com.google.android.libraries.places.api.net.FindCurrentPlaceResponse)4 PlaceLikelihood (com.google.android.libraries.places.api.model.PlaceLikelihood)3 RequiresPermission (android.support.annotation.RequiresPermission)1 RequiresPermission (androidx.annotation.RequiresPermission)1 WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 ApiException (com.google.android.gms.common.api.ApiException)1 LatLng (com.google.android.gms.maps.model.LatLng)1 MarkerOptions (com.google.android.gms.maps.model.MarkerOptions)1 List (java.util.List)1