You can use the Places Service and the Geocoding API with the Maps SDK for Android to search for regions, and get more information about places. The Places Service and Geocoding API are powerful and stable alternatives for obtaining place IDs. If you're already using place IDs, you can reuse those IDs with data-driven styling for boundaries.
Add the Places Service and Geocoding to your Maps SDK for Android apps in the following ways:
- Places SDK for Android is an Android library containing methods to returns information about places.
- Places API returns information about places using HTTP requests.
- Geocoder class can geocode and reverse geocode dynamically from user input.
- Geocoding API lets you geocode static, known addresses.
Use the Places Service
Use Text Search (New) to find a place ID
You can use Text Search (New)
REST API in the Places API to get a place ID that includes region
data by specifying places.id
in the field mask. Usage of the
Text Search (New) to request place IDs only incurs
no charge. Learn more.
For example, to get the place ID for Trinidad, CA you can make the following API call:
curl -X POST -d '{ "textQuery" : "Trinidad, CA" }' \ -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \ -H 'X-Goog-FieldMask: places.id' \ 'https://places.googleapis.com/v1/places:searchText'
Use Places Autocomplete to find regions
The Places Autocomplete
service in the Places SDK for Android provides a convenient way to let
your users search for regions. To configure the Places Autocomplete service to
return only regions, use
AutocompleteSupportFragment.setTypesFilter(List)
to set the type filter to PlaceTypes.REGIONS
.
Get place details for a region
The Place Details service in the Places SDK for Android returns data for a region can be quite useful. For example, you can:
- Search for boundary place IDs based on place names.
- Get the viewport for zooming to a boundary.
- Get the feature type for the boundary (for example
locality
). - Get the formatted address, which resolves to "Place Name, State, Country" in the United States region (for example, "Ottumwa, IA, USA").
- Get other useful data such as photos.
Use the Geocoding API
The Geocoding API lets you convert an address into latitude and longitude coordinates and a Place ID, or converts latitude and longitude coordinates or a Place ID into an address. The following uses combine well with data-driven styling for boundaries:
- Use Geocoding to get the viewport for a region.
- Apply component filtering to your Geocoding call to get the place IDs for administrative areas 1-4, locality, or postal code.
- Use reverse geocoding to find place IDs by latitude and longitude coordinates, or even return place IDs for all components in a particular location.
The following example uses an address (url-escaped) to make a request to the Geocoding API:
https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Amphitheatre%20Pkwy%20Mountain%20View%20CA&key=YOUR_API_KEY
You can use reverse geocoding to find place IDs. The following example Geocoding service function returns the place IDs for all address components at the specified latitude and longitude coordinates:
https://maps.googleapis.com/maps/api/geocode/json?latlng=41.864182,-87.676930&key=YOUR_API_KEY
Use reverse geocoding with component filtering to get the address component for one or more of the following types at the specified location:
administrativeArea
country
locality
postalCode
The next example function shows using the Geocoding service, adding component
restrictions with reverse geocoding to get all of the address components at the
specified location for only the locality
type:
https://maps.googleapis.com/maps/api/geocode/json?latlng=41.864182,-87.676930&result_type=locality&key=YOUR_API_KEY