保存在Android的MapFragment(地图V2)国家 [英] Saving MapFragment (Maps v2) State in Android

查看:254
本文介绍了保存在Android的MapFragment(地图V2)国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用新的GoogleMaps API(V2)工作,有一些问题的片段。我有两个片段在我的应用程序(地图列表),用户可以在它们之间进行切换。正常工作到目前为止这一code:

 如果(itemPosition == 0){
        getSupportFragmentManager()调用BeginTransaction()
                .replace(R.id.fragmentpos,myMapFragment).commit();
    }否则如果(itemPosition == 1){
        getSupportFragmentManager()调用BeginTransaction()
                .replace(R.id.fragmentpos,myListFragment).commit();
} ......

当我与地图工作,切换到列表中的片段,然后回到地图片段,在地图的最后一个位置不被保存。这不是我想要的,我想,该图是在保存状态,因为我左呢..
我怎样才能做到这一点?我试图保存在摄像机位置

 公共无效的onSaveInstanceState(捆绑outState){
   outState.putDouble(LAT,mMap.getCameraPosition()target.latitude);
   outState.putDouble(LON,mMap.getCameraPosition()target.longitude);
   outState.putFloat(熊,mMap.getCameraPosition()轴承);
   ...

}

和它onCreateView()恢复,但捆绑总是空。如果我更换了片段的onSaveInstanceState不叫。

所以,我怎么能保存地图的状态?


解决方案

  

我怎样才能做到这一点?


如果您替换()片段,老段的意见被破坏,这需要你的图形页面和,presumably,在 CameraPosition

的onSaveInstanceState()主要是为配置更改,如屏幕旋转。 MapFragment SupportMapFragment 已经保留了 CameraPosition (其中,顺便说一句是 Parcelable ,这样可以节省在整个对象中的捆绑而不是零碎的)。

您可以考虑使用显示()隐藏()而不是更换( ),所以片段其观点坚持围绕。

I'm working with the new GoogleMaps API (v2) and have some problems with the fragment. I have two fragments in my app (Map, List) and the user can switch between them. Works fine so far with this code:

if (itemPosition == 0) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentpos, myMapFragment).commit();
    } else if (itemPosition == 1) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentpos, myListFragment).commit();
}......

When I work with the map, switch to the list fragment and then back to the map fragment, the last position of the map is not saved. This is not what i want, i want that the map is in the save state as i "left" it.. How can I accomplish this? I tried to save the camera position in

public void onSaveInstanceState(Bundle outState){
   outState.putDouble("LAT", mMap.getCameraPosition().target.latitude);
   outState.putDouble("LON", mMap.getCameraPosition().target.longitude);
   outState.putFloat("BEAR", mMap.getCameraPosition().bearing);
   ...

}

and restore it in onCreateView(), but the Bundle is always null. The onSaveInstanceState is not called if I replace the fragments.

So how can I save the state of the map?

解决方案

How can I accomplish this?

If you replace() a fragment, the old fragment's views are destroyed, which takes out your MapView and, presumably, the CameraPosition.

onSaveInstanceState() is mostly for configuration changes, such as screen rotations. MapFragment and SupportMapFragment already retain the CameraPosition (which, BTW, is Parcelable, so you can save the whole object in the Bundle rather than piecemeal).

You could consider using show() and hide() instead of replace(), so the fragment and its views sticks around.

这篇关于保存在Android的MapFragment(地图V2)国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆