Using the Google Places API for Android, you can discover the place where the device is currently located. That is, 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
PlaceDetectionApi.getCurrentPlace(),
you must request the
ACCESS_FINE_LOCATION
permission.
New permissions model in Android 6.0 Marshmallow
Android 6.0 Marshmallow introduces a new model for handling permissions, which streamlines the process for users when they install and upgrade apps. Provided you're using version 8.1 or later of Google Play services, you can configure your app to target the Android 6.0 Marshmallow SDK and use the new permissions model.
If your app supports the new permissions model, the user does not have to grant any permissions when they install or upgrade the app. Instead, the app must request permissions when it needs them at runtime, and the system shows a dialog to the user asking for the permission.
To learn more, see the documentation for Android 6.0 Marshmallow and the changes you must make to your app for the new permissions model.
Usage limits
Use of the
PlaceDetectionApi.getCurrentPlace()
method is subject to tiered query limits. See the documentation on
usage limits.
Get the current location
To find the local business or other place where the device is currently
located, call
PlaceDetectionApi.getCurrentPlace().
You can optionally specify a
PlaceFilter
to restrict the results to one or more place IDs (up to a maximum of 10), or
to select only places that are currently open. If no filter is specified,
the results are not filtered.
The API returns a
PlaceLikelihoodBuffer
in a
PendingResult.
The
PlaceLikelihoodBuffer
contains a list of
PlaceLikelihood
objects representing likely places. For each place, the result includes an
indication of the likelihood that the place is the right one. The buffer may
be empty, if there is no known place corresponding to the filter criteria.
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.
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i(TAG, String.format("Place '%s' has likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
likelyPlaces.release();
}
});
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 and 1.0.
- The sum of the likelihoods in a given
PlaceLikelihoodBufferis always less than or equal to 1.0. Note that the sum isn't necessarily 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
PlaceLikelihoodBuffer
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
PlaceDetectionApi.getCurrentPlace(),
the app must also display attributions. See the documentation on
attributions.