The place picker is a simple and yet flexible built-in UI widget, part of the Google Places API for Android.
Introducing the place picker
The
PlacePicker
provides a UI dialog that displays an interactive map and a list of nearby
places, including places corresponding to geographical addresses and local
businesses. Users can choose a place, and your app can then retrieve the
details of the selected place.
The place picker provides the following advantages over developing your own UI widget:
- The user experience is consistent with other apps using the place picker, including Google apps and third parties. This means users of your app already know how to interact with the place picker.
- The map is integrated into the place picker.
- Accessibility is built in.
- It saves development time.
The place picker features autocomplete functionality, which displays place predictions based on user search input. This functionality is present in all place picker integrations, so you don't need to do anything extra to enable autocomplete. For more information about autocomplete, see Place Autocomplete.
Permissions
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.
Add a place picker
Here is a summary of the steps required to launch the place picker:
- Use the
PlacePicker.IntentBuilder()to construct anIntent. - If you want to change the place picker's default behavior, you can use
the builder to set the initial latitude and longitude bounds of the map
displayed by the place picker. Call
setLatLngBounds()on the builder, passing in aLatLngBoundsto set the initial latitude and longitude bounds. These bounds define an area called the 'viewport'. By default, the viewport is centered on the device's location, with the zoom at city-block level. - Call
startActivityForResult(), passing it the intent and a pre-defined request code, so that you can identify the request when the result is returned.
The following code snippet launches the place picker:
int PLACE_PICKER_REQUEST = 1; PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
When the user selects a place, you can retrieve the place by calling
PlacePicker.getPlace().
If the user has not selected a place, the method will return null.
You can also retrieve the most recent
bounds of the map by calling
PlacePicker.getLatLngBounds().
The following code snippet retrieves the place that the user has selected:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}
Set custom colors using the material theme
If you set custom colors in your application using the
material
theme, the place picker inherits the colorPrimary and
colorPrimaryDark attributes from the theme. This is useful for
maintaining a consistent brand across your app and the place picker.
Display attributions in your app
When your app displays information obtained via the place picker, the app must also display attributions. See the documentation on attributions.