4

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 i switch the tab b/w two tabs which contain map, it get overlapped. I don't know to overcome this issue.

//code is here for both tab

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMap();
    mMap.setMyLocationEnabled(true);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
    Log.e("locamanager",""+locationManager);
    Criteria criteria=new Criteria(); // object to retrieve provider
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);
    if(location!=null){
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(provider, 400, 1000, this);


 }


@Override
public void onLocationChanged(Location location) {

    latLng = new LatLng(location.getLatitude(), location.getLongitude());
    Log.e("latlng",""+latLng);
    cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
    mMap.animateCamera(cameraUpdate);
    locationManager.removeUpdates(this);

    new ReverseGeocodingTask(getBaseContext()).execute(latLng);

}

Anyone have solution please help me

Thanks on Advance

Venkat
  • 232
  • 3
  • 11

3 Answers3

5

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_fragmentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>

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
    }

}
Ajay
  • 4,850
  • 2
  • 32
  • 44
  • i really stuck to solve overlapping map fragment in tabs if u have any solution than please help me. – Ketan Mehta Jul 30 '13 at 12:35
  • I have the same issue , Use the above code it's running fine and it solves the mapv2 + tabs issue.... – Ajay Jul 30 '13 at 12:37
  • yes but how to use your fragment class in my activity.please and how's look your xml layout.i get error in your class at map_fragg = (LinearLayout) view.findViewById(R.id.map_fragg); – Ketan Mehta Jul 30 '13 at 12:40
  • i open my map activity on two different tabs with handle some condition.on map tab user show full map view and on search tab user show same map with editbox to search any location.but its overlap old map bro. i stuck in this issue last 4 days but not getting any perfect solution so please help me to solve this issue..many thanks to you bro. – Ketan Mehta Jul 30 '13 at 12:46
  • come to chat room here http://chat.stackoverflow.com/rooms/8818/tamil-mobile-app-developers – Ajay Jul 30 '13 at 13:53
  • thank u so much dude..it solve my tabs + mapsV2 issue completely. – Ketan Mehta Jul 31 '13 at 06:57
  • Good god dude that's a hideous solution. – Christopher Perry Sep 21 '13 at 00:06
0

I had the same problem (google maps v2 showed only a grey grid in a tab, but if I load it as a main activity without tabs than it works).

Then I added mMapFragment.onResume() and now it works in a tab! Try this:

mMapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mMapFragment.onResume(); // needed to get the map to display immediately

I've found the solution here: https://stackoverflow.com/a/16960527/185493

Community
  • 1
  • 1
z0kovsky
  • 63
  • 1
  • 10
0

Just use this class .......

public class MapFragment extends SupportMapFragment {

    GoogleMap mapView;
    private Context context;

    @Override
    public void onCreate(Bundle arg0) {
            super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater mInflater, ViewGroup arg1,
                    Bundle arg2) {
            View view = super.onCreateView(mInflater, arg1, arg2);
              setMapTransparent((ViewGroup) view);
              return view;
    }

    private void setMapTransparent(ViewGroup group) {
            int childCount = group.getChildCount();
            for (int i = 0; i < childCount; i++) {
            View child = group.getChildAt(i);

                    if (child instanceof ViewGroup) {
                        setMapTransparent((ViewGroup) child);
                    } else if (child instanceof SurfaceView) {
                        child.setBackgroundColor(0x00000000);
                    }
                }
            }

    @Override
    public void onInflate(Activity arg0, AttributeSet arg1, Bundle arg2) {
            super.onInflate(arg0, arg1, arg2);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            context = getActivity();
            mapView = getMap();

            }
}
Marco Baldelli
  • 3,638
  • 1
  • 22
  • 29