Skip to content Skip to sidebar Skip to footer

Set Current Place In Place Autocomplete Fragment

I want to set the current place into the place autocomplete fragment when the activity is created. I am able to Load Google Maps API and I have successfully implemented Place Autoc

Solution 1:

I think I'm li'l bit late to answer this question. I found the most prominent solution below. maybe it will help someone. Direct solution is setText method of PlaceAutocompleteFragment API. For Detailed answer please go through below answer.

  1. Load the Google Map
  2. Init the PlaceAutocompleteFragment
  3. getCurrentLocation inside onMapReady Method
  4. set current location Marker
  5. Now, Simply set the name of PlaceAutocompleteFragment by calling its setText Method. (e.g. sourcePlace.setText("Current Location");)

Note: This is the simplest method I have found for my requirements. Try it, if it suits your requirement also.

Solution 2:

Despite it is already 3 years later, I decided to answer in case there is someone needs it. The same problem I faced and had to open Places Autocomplete PROGRAMATICALLY.https://developers.google.com/maps/documentation/places/android-sdk/autocomplete#option_2_use_an_intent_to_launch_the_autocomplete_activity Since startActivityForResult is deprecated, I go for the new way to handle activity results here. When you are creating intent to open Autocomplete activity, you should apply setInitialQuery("Hawaii") before building it. You can call the method from activity using LiveData. Here is the whole code inside my activity:

var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codesvaldata: Intent? = result.datadata?.let {
            val place = Autocomplete.getPlaceFromIntent(data)
            makeToast(place.name.toString()) // <- Consume your result
        }
    }
}

privatefunopenSearchAutocomplete() {
    val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG)

    val intent = Autocomplete.IntentBuilder(
        AutocompleteActivityMode.OVERLAY, fields).setCountry("US") //United States
        .setInitialQuery("Hawaii") // <- set initial query here
        .build(this)
    resultLauncher.launch(intent)
}

Solution 3:

        <android.support.v7.widget.CardView
        android:layout_marginTop="60dp"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:id="@+id/cardView">
        <fragment  android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/place_autocomplete_fragment"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            />

put this in your android xml code inside some layout like relative and then add fallowing java code by calling autocom() method in onCreate method and add Permission of location in Mainefest file

privatevoidautocom()
{
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    autocompleteFragment.setOnPlaceSelectedListener(newPlaceSelectionListener() {
        @OverridepublicvoidonPlaceSelected(Place place) {
            LatLng string_location = place.getLatLng();
            String address = (String) place.getAddress();
            String name = (String) place.getName();
            // Log.i("LocationLatlang", String.valueOf(string_location));//Log.i("LocationAddress", address);//Log.i("Locationname", name);//Log.i("pppp", "Place: " + place.getName());

        }

        @OverridepublicvoidonError(Status status) {
            Log.i("ppperror", "An error occurred: " + status);
        }
    });
}

Post a Comment for "Set Current Place In Place Autocomplete Fragment"