in car_app_library/places/common/src/main/java/androidx/car/app/sample/places/common/PlaceListScreen.java [105:183]
public Template onGetTemplate() {
ItemList.Builder listBuilder = new ItemList.Builder();
boolean hasPlaces = false;
// If we don't have any places yet, show a loading progress indicator.
if (mPlaces != null) {
// Add one row per place in the results.
for (int i = 0; i < mPlaces.size(); i++) {
if (i >= 6) {
// only 6 rows allowed.
break;
}
PlaceInfo place = mPlaces.get(i);
Location location = place.getLocation();
int distanceMeters = getDistanceFromSearchCenter(location);
int distanceKm = distanceMeters / METERS_TO_KMS;
SpannableString address =
new SpannableString(
" \u00b7 " + place.getAddress(mGeocoder).getAddressLine(0));
DistanceSpan distanceSpan =
DistanceSpan.create(Distance.create(distanceKm, Distance.UNIT_KILOMETERS));
address.setSpan(distanceSpan, 0, 1, SPAN_INCLUSIVE_INCLUSIVE);
listBuilder.addItem(
new Row.Builder()
// Clicking on the place shows a toast with the full place address.
.setOnClickListener(() -> onClickPlace(place))
.setTitle(place.getName())
.addText(address)
.setMetadata(
new Metadata.Builder()
.setPlace(
new Place.Builder(
CarLocation.create(
location))
.setMarker(
new PlaceMarker.Builder()
.build())
.build())
.build())
.build());
hasPlaces = true;
}
}
// Anchor the map around the search center if there is no place results, or if the anchor
// location has been explicitly set.
Place anchor = null;
if (mAnchor != null) {
anchor =
new Place.Builder(
CarLocation.create(
mAnchor.getLatitude(), mAnchor.getLongitude()))
.setMarker(new PlaceMarker.Builder().setColor(CarColor.BLUE).build())
.build();
} else if (!hasPlaces) {
anchor =
new Place.Builder(
CarLocation.create(
mSearchCenter.getLatitude(),
mSearchCenter.getLongitude()))
.build();
}
PlaceListMapTemplate.Builder builder =
new PlaceListMapTemplate.Builder()
.setTitle(mCategory.getDisplayName())
.setHeaderAction(Action.BACK)
.setAnchor(anchor)
.setCurrentLocationEnabled(true);
if (mPlaces == null) {
return builder.setLoading(true).build();
} else {
return builder.setItemList(listBuilder.build()).build();
}
}