2015-10-20 180 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"/> 

这为我工作