Android Google map v2 in tab Overlapping issue, Multiple map in tab

前端 未结 3 1799
醉话见心
醉话见心 2021-01-13 17:15

I am new to android, now am working with Google maps v2, I have used tabhost to show Google map v2, In this tabhost i need to show two Google maps v2 on different tab. When

3条回答
  •  悲哀的现实
    2021-01-13 18:09

    I have faced the same issue but finally i managed it in this way.

    First of all i create separate MyMapFragment class which extends android.support.v4.app.Fragment

    here is the onCreateView method

    if (view != null) {
    
                ViewGroup parent = (ViewGroup) view.getParent();
                if (parent != null)
                    parent.removeView(view);
            }
    
            try {
                view = (ViewGroup) inflater.inflate(R.layout.map_frag, container,
                        false);
                setUpMapIfNeeded();
                showMap(maptype);
            } catch (Exception e) {
            }
            return view;
    

    after that i create one xml file

    
    
    

    You can add this layout file into your map layout place

    and finally you have to add map view using this one in your activty class

    MyMapFragment newFragment = new MyMapFragment (1);
            FragmentTransaction transaction = getActivity()
                    .getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.my_fragmentView, newFragment);
            transaction.commit();
    

    It's working fine at my side i hope it helps you.

    MapFragment Code is here

    public class MapFragment extends Fragment {
    
        private View view;
        private GoogleMap mMapView;
    
        public TouchableWrapper mTouchView;
        LinearLayout map_fragg;
    
        String userLat, userLon;
    
        int maptype = 1;
    
        Marker lastMarker = null;
    
        public MapFragment() {
        }
    
        public MapFragment(int maptype) {
            this.maptype = maptype;
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            getView();
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            if (container == null) {
                return null;
            }
    
            if (view != null) {
    
                ViewGroup parent = (ViewGroup) view.getParent();
                if (parent != null)
                    parent.removeView(view);
    
                if (mTouchView != null) {
                    mTouchView.removeAllViews();
                }
    
            }
    
            try {
                mTouchView = new TouchableWrapper(getActivity());
                view = (ViewGroup) inflater.inflate(R.layout.map_frag, null, false);
                view.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
    
                map_fragg = (LinearLayout) view.findViewById(R.id.map_fragg);
                mTouchView.addView(view);
                setUpMapIfNeeded();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return mTouchView;
    
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        private void showMap(int maptype) {
    
            switch (maptype) {
            case 1:
                setMapX();
                break;
            case 2:
                setMapXX();
                break;
            case 3:
                setUpMapXXX();
                break;
            default:
                break;
            }
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
        }
    
        private void setUpMapIfNeeded() {
            // Do a null check to confirm that we have not already instantiated the
            // map.
            if (mMapView == null) {
                // Try to obtain the map from the SupportMapFragment.
    
                mMapView = ((SupportMapFragment) getActivity()
                        .getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                // Check if we were successful in obtaining the map.
                if (mMapView != null) {
                    setUpMap();
                    showMap(maptype);
                }
            }
        }
    
        private void setUpMap() {
            mMapView.setMyLocationEnabled(true);
        }
    
        // Setting up map for MyProfile Page
        private void setMapX() {
            // Your Code for adding Markers
        }
    
        // Setting up map for MyProfile Page
        private void setMapXX() {
            // Your Code for adding markers
        }
    
    
        public class TouchableWrapper extends FrameLayout {
    
            public TouchableWrapper(Context context) {
                super(context);
            }
    
            @Override
            public boolean dispatchTouchEvent(MotionEvent ev) {
                switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                    this.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }
                return super.dispatchTouchEvent(ev);
            }
        }
    
        @Override
        public void onStop() {
            super.onStop();
    
            try {
                SupportMapFragment f = (SupportMapFragment) getFragmentManager()
                        .findFragmentById(R.id.map);
                if (f != null)
                    getFragmentManager().beginTransaction().remove(f).commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
        }
    
        @Override
        public View getView() {
            if (view != null) {
                return view;
            }
            return super.getView();
        }
    
    
    
        private void setUpMapXXX() {
            // Your Code for adding Markers
        }
    
    }
    

提交回复
热议问题