【问题标题】:Controlling tile overlays after orientation change in google maps android api v2在谷歌地图android api v2中方向改变后控制平铺覆盖
【发布时间】:2013-08-19 06:22:06
【问题描述】:

我正在使用 google maps android api 开发一个应用程序。该应用程序显示用户可以打开和关闭的各种图块叠加。当我旋转我的设备时,覆盖保持在原位,但是当我单击按钮关闭我的覆盖时,我的设备就像控制我的覆盖的变量为空一样。在这种情况下,我怎样才能保持对我的 tileoverlay 的控制。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    //mMap=null;
    if (savedInstanceState == null) {
        // First incarnation of this activity.
        mapFragment.setRetainInstance(true);
    } else  {
        // Reincarnated activity. The obtained map is the same map instance in the previous
        // activity life cycle. There is no need to reinitialize it.
        mMap = mapFragment.getMap();
    }
    setUpMapIfNeeded();
    setUpFieldsIfNeeded();
}
private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
          // Try to obtain the map from the SupportMapFragment.
             mMap  = ((SupportMapFragment)   getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

          }
        }
      }
 private void setUpFieldsIfNeeded(){            

    if (fieldsOverlay == null) {
        TileProvider tileProvider = new UrlTileProvider(256, 256) {
            @Override
            public synchronized URL getTileUrl(int x, int y, int zoom) {


                //String s = String.format(Locale.US, CMAFieldsRequestFormat, zoom, x, y);  
                String s = String.format(Locale.US, cartoDBFieldsTile, zoom, x, y); 
                s =s.replace(" ", "%20");
                System.out.println(s);
                URL url = null;
                try {
                    url = new URL(s);
                } catch (MalformedURLException e) {
                    throw new AssertionError(e);
                }
                return url;
            }
        };


         fieldsOverlay = mMap.addTileOverlay(
                 new TileOverlayOptions().tileProvider(tileProvider));
         fieldsOverlay.setVisible(false);
         fieldsOverlay.setZIndex(1);}

    }

TLDR:我正在使用带有名为 fieldsOverlay 的 tileOverlay 的谷歌地图。当我旋转我的设备时,覆盖层本身会被保留,但它不再连接到变量 fieldsOverlay。解决此问题的最佳方法是什么。

谢谢

编辑:通过运行 mMap.clear 然后重新初始化我的图层并将可见性设置为 savedInstanceState 值,我能够让应用程序看起来像我想要的那样运行,但这似乎不像一个很好的解决方案。

【问题讨论】:

    标签: android google-maps-android-api-2


    【解决方案1】:

    使用SupportMapFragment.setRetainInstace 似乎是实现预期结果的快速方法,但实际上并非如此。

    首先它会导致内存泄漏。查看我的gmaps-api-issue 进行讨论。

    你遇到的问题很正常。重新创建 Activity 时,您保留的 TileOverlay 引用将不会保留。实际上创建了一个新的Activity 对象。视觉对象应该像普通的 Android Views 一样对待:配置更改时不保留。 the official documentation 中甚至有一个注释,但我现在找不到。

    如果您不使用setRetainInstance,您将不会遇到不引用对象的问题,因为您必须在旋转时再次创建。

    【讨论】:

    • 您的解决方案完美运行。我从 google 的 maps api 演示项目中获取了那行代码,并想警告其他人。
    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多