Current Place

Using the Places SDK for Android, you can discover the place at the device's currently-reported location. Examples of places include local businesses, points of interest, and geographic locations.

Permissions

If your app uses PlacesClient.findCurrentPlace(), users of your app must consent to ACCESS_FINE_LOCATION. The library automatically requests the appropriate permissions for you (you do not need to explictly request them, as they are already declared in the library's manifest):

Read more about permissions and consider using EasyPermissions to get started.

Get the current location

To find the local business or other place where the device is currently located, follow these steps:

  1. Call ContextCompat.checkSelfPermission to check whether the user has granted permission to access their device location. Your app must also include code to prompt the user for permission, and to handle the result. See Request App Permissions for details.
  2. Create a FindCurrentPlaceRequest, passing a List of Place.Fields, specifying the place data types your app should request.
  3. Call PlacesClient.findCurrentPlace(), passing the FindCurrentPlaceRequest you created in the previous step.
  4. Get the list of PlaceLikelihoods from the FindCurrentPlaceResponse.

The API returns a FindCurrentPlaceResponse in a Task. The FindCurrentPlaceResponse contains a list of PlaceLikelihood objects representing places where the device is likely to be located. For each place, the result includes an indication of the likelihood that the place is the right one. The list may be empty, if there is no known place corresponding to the given device location.

You can call PlaceLikelihood.getPlace() to retrieve a Place object, and PlaceLikelihood.getLikelihood() to get the place's likelihood rating. A higher value means a greater probability that the place is the best match.

The following code sample retrieves the list of places where the device is most likely to be located, and logs the name and likelihood for each 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();
}

Notes about the likelihood values:

  • The likelihood provides a relative probability of the place being the best match within the list of returned places for a single request. You can't compare likelihoods across different requests.
  • The value of the likelihood will be between 0.0 and 1.0.

For example, to represent a 55% likelihood that the correct place is Place A, and a 35% likelihood that it's Place B, the response has two members, Place A with a likelihood of 0.55 and Place B with a likelihood of 0.35.

Display attributions in your app

When your app displays information obtained from PlacesClient.findCurrentPlace(), the app must also display attributions. See the documentation on attributions.

Feedback geben zu...