0
所以我有以下的东西都是正常工作:在地图碎片初始位置.setRetainInstance
我有地图碎片,我有我的地图标记。在我的地图片段上使用.setRetainInstance完全符合我的要求,旋转它可以让用户放大位置并将标记保留在其位置上。我现在唯一想做的事情就是让应用程序的初始屏幕将相机移动到精确的位置和缩放级别。我正在用initialLocation方法做到这一点,如果在onMapReady中添加它,它会做它应该做的。问题是,一旦我将这个方法添加到MapReady中,setRetainInstance不再工作,在每次旋转时,贴图将重置为initialLocation位置。正如你可能会从我的代码中意识到的,我只是在学习这一点,而且我已经阅读了很多教程,但是我无法做到正确。这是代码的一部分,所以你可以了解我在说什么。我想我必须添加一些条件才能使其工作。任何建议将不胜感激。
private static final double
TOULOUSE_LAT = 43.604346,
TOULOUSE_LNG = 1.443760;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (savedInstanceState == null){
mapTypeSelected = GoogleMap.MAP_TYPE_NORMAL;
mapFragment.setRetainInstance(true);
} else {
mapTypeSelected = savedInstanceState.getInt("the_map_type", GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
initialLocation(TOULOUSE_LAT,TOULOUSE_LNG, 12);
mMap.setMapType(mapTypeSelected);
addMarkers2Map();// method for adding markers and a lot of other stuff...
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("the_map_type", mapTypeSelected);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.get("the_map_type");
}
private void initialLocation(double lat, double lng, float zoom){
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
}
}
它像一个魅力工作,除了在savedInstanceState == null的事实mSetCameraPosition必须与true和false在else语句中相等。非常感谢! –
很高兴为你效劳! (我更新了我的答案) –