Fetch Google Map Places In Android(Place Picker)



Place Picker ?

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

If your app uses the place picker, you must request the ACCESS_FINE_LOCATION permission.

Add a place picker

Here is a summary of the steps required to launch the place picker:

Use the PlacePicker.IntentBuilder() to construct an Intent.
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 a LatLngBounds to 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 colorPrimaryand colorPrimaryDark attributes from the theme.
 This is useful for maintaining a consistent brand across your app and the place picker.
Fetch Google Map Places In Android(Place Picker) Fetch Google Map Places In Android(Place Picker) Reviewed by Unknown on 4:14 AM Rating: 5

No comments:

Theme images by enjoynz. Powered by Blogger.