Android Places API
Place Picker Usage Example
Place Picker is a really simple UI widget provided by Places API. It provides a built-in map, current location, nearby places, search abilities and autocomplete.
This is a sample usage of Place Picker UI widget.
private static int PLACE_PICKER_REQUEST = 1;
private TextView txtPlaceName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker_sample);
txtPlaceName = (TextView) this.findViewById(R.id.txtPlaceName);
Button btnSelectPlace = (Button) this.findViewById(R.id.btnSelectPlace);
btnSelectPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openPlacePickerView();
}
});
}
private void openPlacePickerView(){
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(this, data);
Log.i(LOG_TAG, String.format("Place Name : %s", place.getName()));
Log.i(LOG_TAG, String.format("Place Address : %s", place.getAddress()));
Log.i(LOG_TAG, String.format("Place Id : %s", place.getId()));
txtPlaceName.setText(String.format("Place : %s - %s" , place.getName() , place.getAddress()));
}
}
}
Getting Current Places by Using Places API
You can get the current location and local places of user by using the Google Places API.
Ar first, you should call the PlaceDetectionApi.getCurrentPlace()
method in order to retrieve local business or other places. This method returns a PlaceLikelihoodBuffer
object which contains a list of PlaceLikelihood
objects. Then, you can get a Place
object by calling the PlaceLikelihood.getPlace()
method.
Important: You must request and obtain the ACCESS_FINE_LOCATION
permission in order to allow your app to access precise location information.
private static final int PERMISSION_REQUEST_TO_ACCESS_LOCATION = 1;
private TextView txtLocation;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
txtLocation = (TextView) this.findViewById(R.id.txtLocation);
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
getCurrentLocation();
}
private void getCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e(LOG_TAG, "Permission is not granted");
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_TO_ACCESS_LOCATION);
return;
}
Log.i(LOG_TAG, "Permission is granted");
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(googleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
Log.i(LOG_TAG, String.format("Result received : %d " , likelyPlaces.getCount() ));
StringBuilder stringBuilder = new StringBuilder();
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
stringBuilder.append(String.format("Place : '%s' %n",
placeLikelihood.getPlace().getName()));
}
likelyPlaces.release();
txtLocation.setText(stringBuilder.toString());
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_TO_ACCESS_LOCATION: {
// If the request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getCurrentLocation();
} else {
// Permission denied, boo!
// Disable the functionality that depends on this permission.
}
return;
}
// Add further 'case' lines to check for other permissions this app might request.
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(LOG_TAG, "GoogleApiClient connection failed: " + connectionResult.getErrorMessage());
}
Place Autocomplete Integration
The autocomplete feature in the Google Places API for Android provides place predictions to user. While user types in the search box, autocomplete shows places according to user’s queries.
AutoCompleteActivity.java
private TextView txtSelectedPlaceName;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autocomplete);
txtSelectedPlaceName = (TextView) this.findViewById(R.id.txtSelectedPlaceName);
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.fragment_autocomplete);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i(LOG_TAG, "Place: " + place.getName());
txtSelectedPlaceName.setText(String.format("Selected places : %s - %s" , place.getName(), place.getAddress()));
}
@Override
public void onError(Status status) {
Log.i(LOG_TAG, "An error occurred: " + status);
Toast.makeText(AutoCompleteActivity.this, "Place cannot be selected!!", Toast.LENGTH_SHORT).show();
}
});
}
}
activity_autocomplete.xml
<fragment
android:id="@+id/fragment_autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtSelectedPlaceName"
android:layout_margin="20dp"
android:padding="15dp"
android:hint="@string/txt_select_place_hint"
android:textSize="@dimen/place_autocomplete_prediction_primary_text"/>
Adding more than one google auto complete activity.
public static final int PLACE_AUTOCOMPLETE_FROM_PLACE_REQUEST_CODE=1;
public static final int PLACE_AUTOCOMPLETE_TO_PLACE_REQUEST_CODE=2;
fromPlaceEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//Do your stuff from place
startActivityForResult(intent, PLACE_AUTOCOMPLETE_FROM_PLACE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
});
toPlaceEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//Do your stuff to place
startActivityForResult(intent, PLACE_AUTOCOMPLETE_TO_PLACE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_AUTOCOMPLETE_FROM_PLACE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Do your ok >from place< stuff here
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
//Handle your error >from place<
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
} else if (requestCode == PLACE_AUTOCOMPLETE_TO_PLACE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Do your ok >to place< stuff here
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
//Handle your error >to place<
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
Setting place type filters for PlaceAutocomplete
In some scenarios, we might want to narrow down the results being shown by PlaceAutocomplete to a specific country or maybe to show only Regions. This can be achieved by setting an AutocompleteFilter on the intent. For example, if I want to look only for places of type REGION and only belonging to India, I would do the following:
MainActivity.java
public class MainActivity extends AppComatActivity {
private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
private TextView selectedPlace;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedPlace = (TextView) findViewById(R.id.selected_place);
try {
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS)
.setCountry("IN")
.build();
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.setFilter(typeFilter)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
final Place place = PlacePicker.getPlace(this, data);
selectedPlace.setText(place.getName().toString().toUpperCase());
} else {
Toast.makeText(MainActivity.this, "Could not get location.", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selected_place"/>
</LinearLayout>
The PlaceAutocomplete will launch automatically and you can then select a place from the results which will only be of the type REGION and will only belong to the specified country. The intent can also be launched at the click of a button.