Map v2 SupportMapFragment inside Viewpager

后端 未结 1 2009
自闭症患者
自闭症患者 2020-12-24 15:12

Background

I have successfuflly added the new map api v2.0 to a ViewPager and the map loads fine. I am using the FragmentViewPager. I run into troub

相关标签:
1条回答
  • 2020-12-24 15:45

    Just tried like this and it works.

    public class MyMapFragment extends SupportMapFragment {
    
        private List<MarkerOptions> mMarkers;
    
        public static MyMapFragment create(GoogleMapOptions options, ArrayList<MarkerOptions> markers) {
            MyMapFragment fragment = new MyMapFragment();
    
            Bundle args = new Bundle();
            args.putParcelable("MapOptions", options); //obtained by decompiling google-play-services.jar
            args.putParcelableArrayList("markers", markers);
            fragment.setArguments(args);
    
            return fragment;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Hacky and ugly but it works
            ArrayList<Parcelable> list = getArguments().getParcelableArrayList("markers");
            mMarkers = new ArrayList<MarkerOptions>(list.size());
            for (Parcelable parcelable : list) {
                mMarkers.add((MarkerOptions) parcelable);
            }
        }
    
        @Override
        public void onResume() {
            super.onResume();
            GoogleMap mMap = super.getMap();
            //add the markers
            if (mMap != null) {
                for (MarkerOptions marker : mMarkers) {
                    mMap.addMarker(marker);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题