android – 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);
}
});
}
}
}
问题是:当我旋转设备时,MapFragments不再重新触摸事件……
有没有人有同样的问题并解决它?
解决方法:
我遇到了同样的问题,经过多次故障排除后发现容器中有多个地图碎片.
所以,我通过使用解决了这个问题
FragmentTransaction.replace
代替
.add
标签:android-maps-v2,android,android-fragments 来源: https://codeday.me/bug/20190901/1782725.html