0
在我的應用程序,我都SupportMapFragment
以及PlaceAutocompleteFragment
和佈局,fragment_map是:移動相機到所選擇的地方位置
<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>
在我FragmentB,我使用它們像這樣:
@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;
}
我遇到兩個問題:
- 相機不移動到選定的地方,即使我可以得到標記到該位置
- 地圖碎片把我帶回到我的當前位置
我確信我遇到片段生命週期的問題但我不確定是否應該將地圖片段替換爲地點片段,並在選擇onPlaceSelected()
時將其添加到後臺堆棧。