Places API (Java)
The previous lesson put a map on screen with markers for nearby grocery stores that RecipeBox already knew about. But what if the user wants to search for a specific store by name — "Fresh Mart" on the other side of town, say? That's what the Places API is for. Where the Maps SDK draws the map itself, Places gives RecipeBox searchable, structured data about real-world locations: names, addresses, coordinates, and other business details. The two APIs complement each other — Maps shows it, Places helps find it.
Adding the Dependency & Initializing
// app/build.gradle
dependencies {
implementation 'com.google.android.libraries.places:places:3.5.0'
}
// In RecipeBox's Application subclass, or MainActivity.onCreate()
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), "YOUR_PLACES_API_KEY");
}
PlacesClient placesClient = Places.createClient(this);
Place Autocomplete for Searching by Name
An AutocompleteSupportFragment embeds a ready-made search box directly into RecipeBox's nearby-stores screen — as the user types, Google returns matching place suggestions.
<!-- fragment_nearby_stores.xml -->
<fragment
android:id="@+id/autocompleteFragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
// NearbyStoresActivity.java
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocompleteFragment);
autocompleteFragment.setPlaceFields(Arrays.asList(
Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
addSelectedPlaceMarker(place);
}
@Override
public void onError(Status status) {
Log.e("RecipeBoxPlaces", "Autocomplete error: " + status);
}
});
Fetching Nearby Places Programmatically
Instead of waiting for a user to search, RecipeBox could also proactively ask "what grocery stores are near this latitude/longitude?" using a nearby-search style call against the Places API. Conceptually, this means sending the user's current coordinates plus a radius and a place-type filter (like "supermarket"), and getting back a ranked list of matching places with their own coordinates and names — the same shape of data the autocomplete flow above hands you, just triggered by location instead of typed text. This is useful for auto-populating the very "nearby grocery stores" list the Maps lesson assumed RecipeBox already had.
Extracting Place Details & Dropping a Marker
Once a place is selected — whether through autocomplete or a nearby search — RecipeBox pulls its name, address, and coordinates, and adds it to the map from the previous lesson:
private void addSelectedPlaceMarker(Place place) {
LatLng latLng = place.getLatLng();
String name = place.getName();
String address = place.getAddress();
if (latLng != null) {
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(name)
.snippet(address));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16f));
}
}
Usage & Billing Considerations
Unlike the basic Maps SDK, the Places API is a metered, paid Google API beyond a limited free monthly quota. Every autocomplete keystroke session and every place-details fetch counts against that quota. For RecipeBox, this is worth knowing before shipping a feature that calls Places aggressively — for example, firing a details request on every single autocomplete suggestion instead of only when the user actually taps one. It's a good habit to check Google's current Places API pricing page before estimating costs for a feature that will see real production traffic.