Map v2 SupportMapFragment inside Viewpager

杀马特。学长 韩版系。学妹 提交于 2019-11-30 00:32: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);
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!