Google Places AutoComplete EditText
A simple library that can connect your autocomplete edittext to Google's places api
Supporting Places AutoComplete EditText
Google Places AutoComplete EditText is an independent project with ongoing development and support made possible thanks to donations made by these awesome backers. If you'd like to join them, please consider:
How to integrate into your app?
Integrating the project is simple a refined all you need to do is follow the below steps
Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.mukeshsolanki:Google-Places-AutoComplete-EditText:<latest-version>'
}
How to use the library?
Okay seems like you integrated the library in your project but how do you use it? Well its really easy just add the following to your xml design
.....
<AutoCompleteTextView
android:id="@+id/autoCompleteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
.....
To connect the AutoCompleteTextView to the places api create a PlaceApi object and attach it to the AutoCompleteTextView like-wise
val placesApi = PlaceAPI.Builder().apiKey("YOUR_API_KEY").build(this@MainActivity)
autoCompleteEditText.setAdapter(PlacesAutoCompleteAdapter(this, placesApi))
That's pretty much it and your all wrapped up. You have successfully connected the Places Api to the AutoCompleteTextView.
Getting details of place
To get the selected address simply set an ItemClickListener
autoCompleteEditText.onItemClickListener =
AdapterView.OnItemClickListener { parent, _, position, _ ->
val place = parent.getItemAtPosition(position) as Place
autoCompleteEditText.setText(place.description)
}
To get the details of the place selected you can set a listener to get the details
placesApi.fetchPlaceDetails("placeId", object : OnPlacesDetailsListener {
override fun onError(errorMessage: String) {
Toast.makeText(this@MainActivity, errorMessage, Toast.LENGTH_SHORT).show()
}
override fun onPlaceDetailsFetched(placeDetails: PlaceDetails) {
//TODO do anything with placeDetails object
}
})
Places details you can get
Details | Value |
---|---|
id | get the record id (not to be confused with the place id) |
name | gets the name of the place |
address | gets a list of different address types like street number, route, locality, country, postal code |
lat | gets the latitude of the place |
lng | gets the longitude of the place |
place id | gets the id of the place |
url | gets the maps url of the place |
utc offset | gets the utc offset of the place |
vicinity | gets the vicinity of the place |
compound plus code | gets the expanded Google plus code |
global plus code | gets the shortened Google plus code |
Author
Maintained by Mukesh Solanki
Contribution
- Bug reports and pull requests are welcome.
- Make sure you use square/java-code-styles to format your code.
License
MIT License
Copyright (c) 2019 Mukesh Solanki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.