更改屏幕方向时,地图不再放大

时间:2014-08-11 19:45:06

标签: android maps

当我在我的Android应用程序上放大地图,然后我将方向从纵向切换到横向时,地图会缩小并且不会保持其放大状态。我希望它能够保持放大,即使我切换到横向模式。任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

通过@miguel

bundle.putDouble("lat", mMap.getCameraPosition().target.latitude); bundle.putDouble("lon", mMap.getCameraPosition().target.longitude); bundle.putFloat("zoom", mMap.getCameraPosition().zoom);

然后在onCreate上,恢复地图状态,如下所示:

bundle.getDouble("lat"); bundle.getDouble("lon"); bundle.getDouble("zoom");

答案 1 :(得分:0)

我认为将此添加到清单中会解决它。

android:configChanges="orientation|keyboardHidden|screenSize"

或者您将保存相机位置,这也将处理用户同时移动地图的位置。有望改变方向尺寸由Google处理。

private CameraPosition currentCameraPosition;
private static final String SAVED_INSTANCE_CAMERA_POSITION = "com.gosylvester.bestrides.cameraPosition";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // save the current camera position;
    if (currentCameraPosition != null) {
        savedInstanceState.putParcelable(SAVED_INSTANCE_CAMERA_POSITION,
                currentCameraPosition);
    }
}


@Override
protected void onPause() {
    super.onPause();
    if (mMap != null) {
        currentCameraPosition = mMap.getCameraPosition();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // First Run checks
    if (savedInstanceState == null) {
        currentCameraPosition = null;
    } else {
        // get the saved_Instance state
        // always get the default when key doesn't exist
        currentCameraPosition = savedInstanceState
                .containsKey(SAVED_INSTANCE_CAMERA_POSITION) ? (CameraPosition) savedInstanceState
                .getParcelable(SAVED_INSTANCE_CAMERA_POSITION) : null;
    }
}

private void setUpMap() {
    final View mapView = getSupportFragmentManager().findFragmentById(
            R.id.map).getView();
    if (mapView.getViewTreeObserver().isAlive()) {
        mapView.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    // We use the new method when supported
                    @SuppressLint("NewApi")
                    // We check which build version we are using.
                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            mapView.getViewTreeObserver()
                                    .removeGlobalOnLayoutListener(this);
                        } else {
                            mapView.getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        }
                        // if there is a cameraPosition then move to it.
                        if (currentCameraPosition != null) {
                            mMap.moveCamera(CameraUpdateFactory
                                    .newCameraPosition(currentCameraPosition));
                        }
                    } 
                });
     }
}