Application where i consume Google Places Search API to display list of places baed on Search criteria
- Use Google API Search POST endpoint to consume api to display list of real locations by name
- Using Post Request to take in a Body and makes a call to api
- How to setup google Places API in Android Studio
Add dependency to build.gradle(Module)
implementation("com.google.android.libraries.places:places:3.5.0")
Initialize Places SDK in your Application Class
@HiltAndroidApp
class Application: Application() {
override fun onCreate() {
super.onCreate()
val apiKey = BuildConfig.PLACES_API_KEY
if (apiKey.isEmpty() || apiKey == "DEFAULT_API_KEY") {
Log.e("Places", "No API key found")
return
}
// Initialize Places SDK
Places.initializeWithNewPlacesApiEnabled(applicationContext, apiKey)
}
}
Set Base URL and endpoint for Places Search API
object ApiDetails {
// APIs -> Post https://places.googleapis.com/v1/places:searchText
// Takes a Body
// Also takes headers, X-Goog-Api-Key, X-Goog-FieldMask
// Add base url and endpoint
const val BASE_URL = "https://places.googleapis.com/"
const val ENDPOINT_PLACES_TXT_SEARCH = "v1/places:searchText"
}
Pass in required body in order to properly hit this api endpoint
interface PlacesTXTSearchApi {
@POST(ApiDetails.ENDPOINT_PLACES_TXT_SEARCH)
suspend fun getPlacesBasedOnText(
@Body request: PlacesTextSearchRequestModel
): PlacesSearchTextModel
}