我在 Fragment 中显示 GoogleMap。当我旋转屏幕时,地图会重新加载。在 GoogleMaps 应用程序中,地图没有重新加载(地图消失,灰屏并再次出现)
我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_map, container,
false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
if(savedInstanceState == null){
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
}
if(googleMap == null){
googleMap = mMapView.getMap();
//map settings
googleMap.setMyLocationEnabled(true);
googleMap.setIndoorEnabled(false);
googleMap.setTrafficEnabled(false);
googleMap.getUiSettings().setTiltGesturesEnabled(false);
googleMap.getUiSettings().setMapToolbarEnabled(false);
}
if(savedInstanceState == null) {
Location myLocation = getMyLocation();
if (myLocation != null) {
double myLatitude = myLocation.getLatitude();
double myLongitude = myLocation.getLongitude();
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(myLatitude, myLongitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
// and ofcouse onResume, OnPause, OnCreated and onLowMemory
我的相机位置被保留,但地图没有。它被重新加载,这需要时间并且看起来很难看。如何防止我的地图像谷歌地图应用程序一样重新加载?
我看了一下这个问题,但我不想禁止改变方向。