如何在同一屏幕中使用PlaceAutocompleteFragment和mapFragment

时间:2018-01-10 06:21:55

标签: android google-maps mapfragment placeautocompletefragment

这是我的.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" />

这是我的.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);
            }
        });

我需要根据文本视图中的输入加载一些位置。但它没有加载。我认为问题是在同一个文件中使用了两个片段。如何加载位置建议以及如何在mapfragment中使用PlaceAutocompleteFragment。

1 个答案:

答案 0 :(得分:0)

试试这个

    <?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代码

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();
  }
}