2

Is it possible to use the Google places API in this layout:

layout

I highlighted the EditText field where i want to use the API in.

I've already searched for numerous guides on how to do this but it seems all of them use deprecated code. I'm also fairly new to Fragments but i would like to get to know more of this topic.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Vito Mastroianni
  • 87
  • 1
  • 1
  • 7

2 Answers2

3

This is possible if you use the Web API, but will require a lot of work.

However, it looks like someone has done it before: https://github.com/mukeshsolanki/Google-Places-AutoComplete-EditText

Alternatively, you can use the Places SDK for Android, which provides the AutoCompleteSupportFragment and AutoCompleteActivity.

Vivek Vinodh
  • 114
  • 1
  • 13
0

set onclicklistener on that edittext and in onclick method call below function

private void searchPlace() { 
if (!Places.isInitialized(){ Places.initialize(this, getApplication().getString(R.string.google_maps_key)); } 
// Set the fields to specify which types of place data to 
// return after the user has made a selection. 
List<Place.Field> fields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.ADDRESS);
 // Start the autocomplete intent. 
Intent intent = new Autocomplete.IntentBuilder( AutocompleteActivityMode.FULLSCREEN, fields) .build(this);
 startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE); }

And get selected result in onresult method

Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                destinationAddress = place.getAddress();
                lblDestinationAddress.setText(destinationAddress);

            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Toast.makeText(this, "Some went wrong. Search again", Toast.LENGTH_SHORT).show();
                Log.i(TAG, status.getStatusMessage());
            } 

Dependency

//search places implementation
 'com.google.android.libraries.places:places:2.0.0'
kam1234
  • 574
  • 2
  • 5
  • 9
  • I don't think this is what he is looking for – Vivek Vinodh Oct 29 '19 at 20:05
  • Recently, i have added same feature in my app where user click on edittext and a Google custom search view will pop up – kam1234 Oct 29 '19 at 20:10
  • I believe he wants the Places suggestions to be shown attached to his EditText. – Vivek Vinodh Oct 29 '19 at 20:11
  • [See this link](https://stackoverflow.com/a/45109604) i think he wants something like that – kam1234 Oct 29 '19 at 20:15
  • Thanks for the replies, @VivekVinodh is right i want to have the places suggestions to be shown in my edittext – Vito Mastroianni Oct 30 '19 at 00:19
  • You can do one thing that when you click on edittext it will call a function which will show a Dialog and when you type any place name it will show list of related places and when you select one out them that place will set on your edittext text – kam1234 Oct 30 '19 at 00:29
  • Or check this tutorial https://medium.com/skillhive/android-google-places-autocomplete-feature-bb3064308f05 – kam1234 Oct 30 '19 at 00:37