1

i create my project for ride booking app. I use AutocompleteSupportFragment for search place . After selecting Place , i filter that location if you are in Delhi then set Text of AutocompleteSupportFragment other wise set Hint like "Please select valid place".

I try

autocompleteFragment_to.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place place) {

            if (!getStateName(place.getLatLng().latitude, place.getLatLng().longitude).equals("Madhya Pradesh")) {
                Common.myLatLong = null;
                Common.placeName1 = null;
                Common.placeName1City = null;
                Log.e("FRagmentData",""+autocompleteFragment_to.a.getText().toString());
                autocompleteFragment_to.a.getText().toString();
                autocompleteFragment_to.a.setText("");
                autocompleteFragment_to.a.setHint("Please select Valid Place");
                autocompleteFragment_to.getView().findViewById(R.id.places_autocomplete_clear_button).performClick();
                autocompleteFragment_to.a.setText("");
                Toast.makeText(context, "Please select Valid Place", Toast.LENGTH_SHORT).show();
            } else {
                destination = place.getLatLng();
                Common.myLatLong = place.getLatLng();
                Common.placeName1 = place.getName();
                Log.e("FRagmentData",""+autocompleteFragment_to.a.getText().toString());
                Common.placeName1City = getAddress(context, place.getLatLng().latitude, place.getLatLng().longitude);
                if (mMap != null) {
                    mMap.clear();
                }
                mapFragment.getMapAsync(MainActivity.this);

            }


        }

        @Override
        public void onError(@NonNull Status status) {
            Toast.makeText(context, "try again later", Toast.LENGTH_SHORT).show();
        }
    });

I try all these approch but it's not working

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Maulik Dadhaniya
  • 157
  • 1
  • 13

1 Answers1

0

Use AutoComplete IntentBuilder instead of fragment..

Just initialise Places inside oncreate()

// Initialize Places.
    Places.initialize(getApplicationContext(), "***YOUR API KEY***");

   // Create a new Places client instance.
   PlacesClient placesClient = Places.createClient(this);

then call intentbuilder in textview.onclick()

List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(
        AutocompleteActivityMode.FULLSCREEN, fields)
        .build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

onActivity result you will get selected place.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }

After that, just set the selected place in the Textview.

Textview.setText(place.getName());

refer:https://stackoverflow.com/a/55045772/10579969

Navin Kumar
  • 3,393
  • 3
  • 21
  • 46