MapFragment становится не отзывчивым после поворота


Моя задача проста, я добавляю 4 FrameLayout к моему XML и добавляю 4 MapFragment кодом в этих контейнерах.

Вот так:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/map_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:orientation="horizontal">
    <FrameLayout
      android:id="@+id/map1_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5""/>
    <FrameLayout
      android:id="@+id/map2_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5"/>
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:orientation="horizontal">
    <fragment
      android:id="@+id/map3_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5"/>
    <fragment
      android:id="@+id/map4_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5""/>
  </LinearLayout>

И в моей деятельности

public class MultiGoogleMapActivity extends FragmentActivity {

    private GoogleMap mMap;
    private GoogleMap mMapUp;
    private GoogleMap mMapMiddle;
    private GoogleMap mMapLow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_google_map);

mMap1Fragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map1_container, mMap1Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
mMap2Fragment = SupportMapFragment.newInstance(new GoogleMapOptions().zoomControlsEnabled(false).zoomGesturesEnabled(false).compassEnabled(false).rotateGesturesEnabled(false).scrollGesturesEnabled(false));
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map2_container, mMap2Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
mMap3Fragment = SupportMapFragment.newInstance(new GoogleMapOptions().zoomControlsEnabled(false).zoomGesturesEnabled(false).compassEnabled(false).rotateGesturesEnabled(false).scrollGesturesEnabled(false));
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map3_container, mMap3Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
mMap4Fragment = SupportMapFragment.newInstance(new GoogleMapOptions().zoomControlsEnabled(false).zoomGesturesEnabled(false).compassEnabled(false).rotateGesturesEnabled(false).scrollGesturesEnabled(false));
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map4_container, mMap4Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();

        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.
        if (mMap == null) {
            mMap = mMap1Fragment.getMap();
        }
        if (mMapUp == null) {
            mMapUp = mMap2Fragment.getMap();
            mMapUp.setOnMapClickListener(new OnMapClickListener() {

                public void onMapClick(LatLng arg0) {
                    CameraPosition cp = mMapUp.getCameraPosition();
                    mMapUp.moveCamera(CameraUpdateFactory.newCameraPosition(mMap.getCameraPosition()));
                    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
                }
            });

        }
        if (mMapMiddle == null) {
            mMapMiddle = mMap3Fragment.getMap();
            mMapMiddle.setOnMapClickListener(new OnMapClickListener() {

                public void onMapClick(LatLng arg0) {
                    CameraPosition cp = mMapMiddle.getCameraPosition();
                    mMapMiddle.moveCamera(CameraUpdateFactory
                            .newCameraPosition(mMap.getCameraPosition()));
                    mMap.moveCamera(CameraUpdateFactory
                            .newCameraPosition(cp));
                }
            });
        }
        if (mMapLow == null) {
            mMapLow = mMap4Fragment.getMap();
            mMapLow.setOnMapClickListener(new OnMapClickListener() {

                public void onMapClick(LatLng arg0) {
                    CameraPosition cp = mMapLow.getCameraPosition();
                    mMapLow.moveCamera(CameraUpdateFactory
                            .newCameraPosition(mMap.getCameraPosition()));
                    mMap.moveCamera(CameraUpdateFactory
                            .newCameraPosition(cp));
                }
            });
        }
        initializeMapsPositions();
    }

    private void initializeMapsPositions() {
        if (mMap != null) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    47.224217, -1.631409), 16));
        }
        if (mMapUp != null) {
            mMapUp.setOnCameraChangeListener(new OnCameraChangeListener() {

                public void onCameraChange(CameraPosition arg0) {
                    mMapUp.animateCamera(CameraUpdateFactory.newLatLngBounds(
                            new LatLngBounds(new LatLng(-21.417276,55.039925),
                                        new LatLng(-20.815174,56.008095)),
                            10));
                    mMapUp.setOnCameraChangeListener(null);
                }
            });

        }
        if (mMapMiddle != null) {
            mMapMiddle.setOnCameraChangeListener(new OnCameraChangeListener() {

                public void onCameraChange(CameraPosition arg0) {
                    mMapMiddle.animateCamera(CameraUpdateFactory.newLatLngBounds(
                            new LatLngBounds(new LatLng(14.337573,-61.441246), 
                                            new LatLng(14.947439,-60.603539)), 
                            10));
                    mMapMiddle.setOnCameraChangeListener(null);
                }
            });
        }
        if (mMapLow != null) {
            mMapLow.setOnCameraChangeListener(new OnCameraChangeListener() {

                public void onCameraChange(CameraPosition arg0) {
                    mMapLow.animateCamera(CameraUpdateFactory.newLatLngBounds(
                            new LatLngBounds(new LatLng(17.596903,-78.448288), 
                                            new LatLng(18.643643,-76.207077)), 
                            10));
                    mMapLow.setOnCameraChangeListener(null);
                }
            });

        }

    }

}

Проблема заключается в следующем: когда я поворачиваю устройство, фрагменты карты больше не реагируют на события касания...

У кого-нибудь была такая же проблема и он решил ее?

1 3

1 ответ:

У меня была та же проблема, и после долгих поисков и устранения неполадок обнаружилось, что в контейнере было несколько фрагментов карты.

Итак, я решил задачу с помощью

FragmentTransaction.replace

Вместо

.add