В моем приложении, у меня есть и 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()
.