Google Maps API v2多个地图片段问题

时间:2013-05-05 16:54:53

标签: java android google-maps android-fragments multimap

我已经尝试了一些新的谷歌地图api v2

我有一个包装mapFragment的片段,这是在应用程序的乞讨时创建的。

在用户点击按钮时创建的另一个片段,此内容为其他mapFragment。

但是这张地图显示了第一个片段上显示的第一张地图。它也被冻结了,不能对它采取行动......

我读过一些用户在显示多地图时遇到问题。知道怎么解决这个问题?

这是我创建地图的方式:

mMapFragment = (SupportMapFragment) this.getActivity().getSupportFragmentManager()
            .findFragmentByTag(MAP_FRAGMENT_TAG);

    // We only create a fragment if it doesn't already exist.
    if (mMapFragment == null) {
        // To programmatically add the map, we first create a
        // SupportMapFragment.
        mMapFragment = SupportMapFragment.newInstance();
        mMapFragment.setRetainInstance(false);
        // Then we add it using a FragmentTransaction.
        FragmentTransaction fragmentTransaction = this.getActivity().getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.add(R.id.ly_map, mMapFragment,MAP_FRAGMENT_TAG);
        fragmentTransaction.commit();
    } else {
        dbug.log("Restoring map");
        mMapFragment.setRetainInstance(false);
        mMap = mMapFragment.getMap();
    }

    // We can't be guaranteed that the map is available because Google Play
    // services might
    // not be available.
    setUpMapIfNeeded();

1 个答案:

答案 0 :(得分:3)

没有人回答,所以我终于自己成功了。

所以我将与其他人分享同样的问题。 如果你有几个片段并且你尝试将两个或多个地图(谷歌地图)接缝实例化以获取之前保存的相同实例,即使您使用了SupportMapFragment.newInstance()

,也会发现存在错误。

无论如何,你应该做的是在打开新地图之前总是关闭地图,所以如果要显示第二个地图,首先要删除前一个地图的片段。然后实例化一个新的并添加到对应的布局。

这不是一个非常好的解决方案,因为它意味着每次都打开和关闭地图,但这是我能找到的最好的。