Move camera to selected Places location

时间:2016-10-20 18:58:56

标签: android-fragments android-maps-v2

In my app, I have both SupportMapFragment as well as PlaceAutocompleteFragment and the layout, fragment_map is:

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <fragment
        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.FragmentB">
        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="top"
            android:layout_margin="5dp"
            android:layout_width="340dp"
            android:layout_height="40dp"
            card_view:cardCornerRadius="4dp">
            <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" />
        </android.support.v7.widget.CardView>
    </fragment>
</LinearLayout>

And in my FragmentB, I use them like so:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    // don't recreate fragment every time
    if (supportMapFragment == null && view == null) {
        view = inflater.inflate(R.layout.fragment_map, container, false);
        supportMapFragment = (SupportMapFragment) this.getChildFragmentManager()
                                .findFragmentById(R.id.map);

        //when successful, calls onMapReady()
        supportMapFragment.getMapAsync(this);
    }

    //Place fragment
    autocompleteFragment = (PlaceAutocompleteFragment) getActivity().
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    // Register a listener to receive callbacks when a place has been selected or an error has occurred
    autocompleteFragment.setOnPlaceSelectedListener(this);
    return view;
}

I run into two issues:

  1. camera doesn't move to the selected place, even though I can get the marker to that location
  2. map fragment takes me back to my current location

I am certain I am running into fragment lifecycle issue but I am not sure if I should replace map fragment with place fragment and add it to the backstack whenever onPlaceSelected() is selected.

1 个答案:

答案 0 :(得分:0)

我为每个片段生命周期调用添加了日志,并注意到onResume地图片段覆盖了地点片段,因为我检查地图客户端是否已连接,因此添加此检查,if (!autocompleteFragment.isVisible()) { //if p if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) { mGoogleApiClient.connect(); Log.d(TAG, "onResume(), called mGoogleApiClient.connect()"); } }确保位置片段具有焦点,因此相机移动到选定的位置。