我创建了一个Android应用程序:
当用户第一次访问该应用程序时,该应用程序可以正常工作。
但当他转动设备屏幕/改变方向时, 地图无法初始化。
在我的onResume方法中,我初始化了mapFragment,如果它不等于null,我会使用getMap()来初始化我的GoogleMap地图属性。
public class MapsActivity extends ActionBarActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
...
@Override
protected void onResume() {
Log.d(LOG_TAG, "onResume");
super.onResume();
if(googleApiClient != null) {
googleApiClient.connect();
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if(mapFragment != null) {
Log.d(LOG_TAG, "mapFragment initialized");
} else {
Log.d(LOG_TAG, "error during mapFragment initialization");
}
map = mapFragment.getMap();
if(map != null) {
Log.d(LOG_TAG, "map initialized");
} else {
Log.d(LOG_TAG, "error during map initialization");
}
}
Log.d(LOG_TAG, "end of onResume");
}
当用户访问该活动时,它正在运行:
12-17 09:35:02.306 25099-25099/com.xxx.xx D/LogMe﹕ onCreate
12-17 09:35:03.746 25099-25099/com.xxx.xx D/LogMe﹕ end of onCreate
12-17 09:35:03.748 25099-25099/com.xxx.xx D/LogMe﹕ onResume
12-17 09:35:03.759 25099-25099/com.xxx.xx D/LogMe﹕ mapFragment initialized
12-17 09:35:03.760 25099-25099/com.xxx.xx D/LogMe﹕ map initialized
12-17 09:35:03.760 25099-25099/com.xxx.xx D/LogMe﹕ end of onResume
12-17 09:35:03.844 25099-25099/com.xxx.xx D/LogMe﹕ onConnected
12-17 09:35:03.848 25099-25099/com.xxx.xx D/LogMe﹕ google play services available
12-17 09:35:03.850 25099-25099/com.xxx.xx D/LogMe﹕ setUpMapIfNeeded
12-17 09:35:03.850 25099-25099/com.xxx.xx D/LogMe﹕ we have a map
12-17 09:35:03.865 25099-25099/com.xxx.xx D/LogMe﹕ setUpMap
12-17 09:35:03.866 25099-25099/com.xxx.xx D/LogMe﹕ location: Location[mProvider=fused,mTime=1418805217845,mLatitude=43.6137752,mLongitude=1.4302528,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=52.5,mExtras=null]
12-17 09:35:03.867 25099-25099/com.xxx.xx D/LogMe﹕ we retrieve markers thx to the web service
但是当他旋转设备屏幕时:
12-17 09:35:25.118 25099-25099/com.xxx.xx D/LogMe﹕ onSaveInstanceState
12-17 09:35:25.119 25099-25099/com.xxx.xx D/LogMe﹕ end of onSaveInstanceState
12-17 09:35:25.260 25099-25099/com.xxx.xx D/LogMe﹕ onCreate
12-17 09:35:25.427 25099-25099/com.xxx.xx D/LogMe﹕ lat et lng: lat/lng: (43.613774948887695,1.4302530884742737)
12-17 09:35:25.428 25099-25099/com.xxx.xx D/LogMe﹕ zoom: 13.0
12-17 09:35:25.430 25099-25099/com.xxx.xx D/LogMe﹕ markers retrieved
12-17 09:35:25.462 25099-25099/com.xxx.xx D/LogMe﹕ end of onCreate
12-17 09:35:25.470 25099-25099/com.xxx.xx D/LogMe﹕ onResume
12-17 09:35:25.609 25099-25099/com.xxx.xx D/LogMe﹕ mapFragment initialized
12-17 09:35:25.609 25099-25099/com.xxx.xx D/LogMe﹕ error during map initialization
12-17 09:35:25.609 25099-25099/com.xxx.xx D/LogMe﹕ end of onResume
12-17 09:35:25.732 25099-25099/com.xxx.xx D/LogMe﹕ onConnected
12-17 09:35:25.735 25099-25099/com.xxx.xx D/LogMe﹕ google play services available
12-17 09:35:25.736 25099-25099/com.xxx.xx D/LogMe﹕ setUpMapIfNeeded
12-17 09:36:28.522 25099-25099/com.xxx.xx D/LogMe﹕ onSaveInstanceState
无法初始化地图。
有你的想法吗?
由于
答案 0 :(得分:0)
您可以尝试以这种方式获取地图:
protected void onResume() {
if (mMap != null) {
// Do markers
return;
}
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap == null) {
return;
}
}
此外,还有一个名为hello-map
here的示例项目。它显示map rotation.
答案 1 :(得分:0)
避免使用XML构建mapView / SupportMapFragment。使用FrameLayout,使用SupportMapFragment的新实例替换。你可以用它做更多的事情(在ChildFragmentManager上添加更多......)。
private MapView mMapView;
private SupportMapFragment mSupportMapFragment;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
createMapFragmentIfNeeded();
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void createMapFragmentIfNeeded() {
mFragmentManager = getChildFragmentManager();
if (mSupportMapFragment == null) {
mSupportMapFragment = createMapFragment();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.map_view, mSupportMapFragment);
fragmentTransaction.commit();
}
}
public void setUpMapIfNeeded() {
if (mMap == null && mSupportMapFragment != null) {
mMap = mSupportMapFragment.getMap();
if (mMap != null) {
//Setup map
}
}
}
protected SupportMapFragment createMapFragment() {
try {
return SupportMapFragment.class.newInstance();
} catch (java.lang.InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
答案 2 :(得分:0)
我用新创建的RetainMapFragment:
类解决了我的问题import android.os.Bundle;
import com.google.android.gms.maps.MapFragment;
public class RetainMapFragment extends MapFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
我在布局中使用了这个新类:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="3"
android:layout_margin="5px"
class="com.xxx.xxx.RetainMapFragment"/>