0

This is my .xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.root.googlemap.MapsActivity" />

    <fragment
        android:id="@+id/autocomplete_fragment"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

This is my .java

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                Log.i(TAG,"Place"+ place.getName());
            }

            @Override
            public void onError(Status status) {
                Log.i(TAG, "Status" +status);
            }
        });

I need to load the some locations based on my input in text view. But it is not loaded. I think the problem is Two fragments are used in same file. How to Load the location suggestions and how to use PlaceAutocompleteFragment inside the mapfragment.

developer
  • 125
  • 1
  • 2
  • 9

1 Answers1

0

try this

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="techpaliyal.com.googlemapsadvance.MainActivity">
    <!--<fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />-->
    <Button
        android:id="@+id/place_autocomplete_fragment"
        android:onClick="findPlace"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Search Location" />
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:layout_below="@+id/place_autocomplete_fragment"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="techpaliyal.com.googlemapsadvance.MapsActivity" />

</RelativeLayout>

Java Code

public final void findPlace(View view) {

  try {
     IntentBuilder builder = new IntentBuilder();
     LatLng jodhpur = new LatLng(26.2389D, 73.0243D);
     LatLng jaipur = new LatLng(26.9124D, 75.7873D);
     builder.setLatLngBounds(new LatLngBounds(jodhpur, jaipur));
     this.startActivityForResult(builder.build((Activity)this), this.PLACE_PICKER_REQUEST);
  } catch (GooglePlayServicesRepairableException var5) {
     var5.printStackTrace();
  } catch (GooglePlayServicesNotAvailableException var6) {
     var6.printStackTrace();
  }
}
  • I follow your code but the error produced by @+id/place_autocomplete_fragment is not a sibling in the same RelativeLayout – developer Jan 10 '18 at 06:47
  • error is coming because `place_autocomplete_fragment` is in comment.and you are putting fragment layout below `place_autocomplete_fragment`.try after removing `android:layout_below="@+id/place_autocomplete_fragment"`. – Normal1One Jan 10 '18 at 07:29
  • comment `PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.autocomplete_fragment); autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { @Override public void onPlaceSelected(Place place) { Log.i(TAG,"Place"+ place.getName()); } @Override public void onError(Status status) { Log.i(TAG, "Status" +status); } });` – Yogesh Choudhary Paliyal Jan 10 '18 at 07:30
  • Im sorry. I could't understand Your answer. Please explain it by code. – developer Jan 10 '18 at 08:00
  • Please tell what I do next. – developer Jan 10 '18 at 09:24