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語句中相等。非常感謝! –
很高興爲你效勞! (我更新了我的答案) –