2015-10-20 85 views
0

我在我的片段中顯示GoogleMap。當我旋轉屏幕時,地圖會重新加載。在谷歌地圖應用程序映射沒有得到重新加載(地圖消失,灰色的屏幕再次出現)谷歌地圖V2防止地圖重新加載旋轉

我的代碼:

@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 

我的相機現在的位置被保留,但地圖是沒有的。它重新加載,需要時間,看起來很醜。我怎樣才能防止我的地圖像谷歌地圖應用程序重新加載?我看了一下this question,但我不想禁止改變方向。

回答

0

你有沒有嘗試添加

android:configChanges="orientation|screenSize"

到活動中您的清單?

它不一定鎖定方向,它允許android嘗試自己處理配置更改。

如果這不是你要找的內容,你可以嘗試和初始化您的片段後,稱

setRetainInstanceState(true)

,當方向後重新調用數據,但是,很可能是因爲一個方向改變迫使android摧毀並重新創建你的活動,GoogleMaps將不得不重新加載。

0

我建議你在Manifest文件的地圖活動中將屏幕方向設置爲縱向。看下面的例子。

 <activity 
     android:name=".activity.MapActivity" 
     android:label="@string/title_activity_map" 
     android:theme="@style/AppTheme" 
     android:screenOrientation="portrait"/> 

這爲我工作