I've looked again and again on the site for this simple question and didn't find an answer I think it is very generic need Let's say that you have two drop down lists One you want to populate with a list of countries, and once you've selected one - to populate the other with a list of it's regions. Is it even possible with Google Places API? Cheers :)
Google places API - How to get regions of a country?
Sounds like you are trying to do something like this. Doesn't use the Places API, uses the Natural Earth data set in FusionTables. Spain (works for most countries in the dataset) –
Kaitlin
I am also looking for the same. Can you please share with me how finally you achieved this ? –
Toilette
No, it is not possible with Places API.
You have 2 options: Use Another Database
There are a few free databases of country/state lists. Have a look at the following questions for some examples:
List of Cities by Country
https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json
Create your own database
Create your own database of the states in each country. Not recommended, as it would be a waste of precious time.
thank you for your answer @Homeliss you're right about a waste of time creating my own database I'm currently using a hi-breed of geonames & google places, but really wanted to uniform all the Id's , names, borders, consistencies therefore really wanted to make sure there is no way doing it with Google places API... Other services are not a route we're going to take but trying to avoid. thank you for your help. –
Unto
Yes It is possible
Below Code can be used to get states as per filter
IN is Country Code which is required for to search in only in India
REGIONS is filter to get state as we search
Need valid Places API Key
val fields = listOf(Place.Field.ID, Place.Field.NAME) searchLocationForCreatePost = findViewById(R.id.searchLocationForCreatePost) if (!Places.isInitialized()) { Places.initialize(getApplicationContext(), getString(R.string.api_key), Locale.US); } searchLocationForCreatePost.setOnClickListener { val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields) .setCountry("IN") .setTypeFilter(TypeFilter.REGIONS) .build(this) startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE) }
- Get Result in onActivityResult -
-------'
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
when (resultCode) {
Activity.RESULT_OK -> {
data?.let {
val place = Autocomplete.getPlaceFromIntent(data)
Log.i("TAG", "Place: ${place.name}, ${place.id}")
}
}
AutocompleteActivity.RESULT_ERROR -> {
// TODO: Handle the error.
data?.let {
val status = Autocomplete.getStatusFromIntent(data)
Log.i("TAG", status.statusMessage.toString())
}
}
Activity.RESULT_CANCELED -> {
// The user canceled the operation.
}
}
return
}
super.onActivityResult(requestCode, resultCode, data)
}
© 2022 - 2024 — McMap. All rights reserved.