简体   繁体   English

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

[英]Google Maps API v2 multiple map fragment problems

I have experimented few issue with the new google map api v2 我已经尝试了新的谷歌地图api v2的几个问题

I have a fragment that wraps a mapFragment, this is created at the begging of the app. 我有一个包装mapFragment的片段,这是在应用程序的乞讨时创建的。

The in another fragment that is created when the user click on a button, this content an other mapFragment. 在用户单击按钮时创建的另一个片段,此内容为另一个mapFragment。

But this map is showing the first map that is showing on the first fragment. 但是这张地图显示了第一个片段上显示的第一张地图。 An also it get frozen and can't make actions on it... 它也冻结了,不能对它采取行动......

I've read that some users have problems to show multimaps. 我已经读过一些用户在显示多地图时遇到问题。 Any idea how can I solve this? 知道怎么解决这个问题?

This is how I create the maps: 这是我创建地图的方式:

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();

Nobody answer so I finally succed on my own. 没有人回答,所以我终于自己成功了。

So I will share for the others with the same problem. 所以我将与其他人分享同样的问题。 It seams that there is a bug, that if you have several fragments and you try to instanciate two or more maps (Google Maps) seams to take the same instance previously saved, even that you use SupportMapFragment.newInstance() 如果你有几个片段并且你尝试将两个或多个地图(谷歌地图)接缝实例化以获取之前保存的相同实例,即使您使用了SupportMapFragment.newInstance(),也会发现存在错误。

Anyway, what you should do is always close the map before open a new one, so if you want to show the second map, first of all remove the fragment with the previous map. 无论如何,你应该做的是在打开新地图之前总是关闭地图,所以如果要显示第二个地图,首先要删除前一个地图的片段。 And then instantiate a new on and add to the correspondent layout. 然后实例化一个新的并添加到对应的布局。

It's not a really good solution because it means to open and close the maps everytime, but is the best I could find. 这不是一个非常好的解决方案,因为它意味着每次都打开和关闭地图,但这是我能找到的最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM