7

Using below code to show and hide MapFragment, and it works just well:

public class MapFragmentActivity extends FragmentActivity {
...........
mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map));
googleMap = mMapFragment.getMap();  
googleMap.setMyLocationEnabled(true);
.....
if(isChecked)
  {                                 
        mMapFragment.getView().setVisibility(View.VISIBLE);                                     
  }
  else 
  {
        mMapFragment.getView().setVisibility(View.GONE);
  }

but whenever i am using it with Animation, Map never hides, it always visible, whereas animation works for me;

if(isChecked)
    {               
        mMapFragment.getView().setVisibility(View.VISIBLE);
        mMapFragment.getView().startAnimation(AnimationUtils.loadAnimation(MapFragmentActivity.this,
         R.anim.slide_up));
    }
    else 
    {
        mMapFragment.getView().setVisibility(View.GONE);
        mMapFragment.getView().startAnimation(AnimationUtils.loadAnimation(MapFragmentActivity.this,
         R.anim.slide_down));
    }
Sun
  • 6,768
  • 25
  • 76
  • 131

4 Answers4

11

You can try this

      private GoogleMap mMap;
    private SupportMapFragment mMapFragment;

if(isCheked) {
       mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
    mMap = mMapFragment.getMap();
    
    mMapFragment.getView().setVisibility(View.Visible);
 
}
else {
   mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
    mMap = mMapFragment.getMap();
    
    mMapFragment.getView().setVisibility(View.INVISIBLE);

}

Or the easy way is doing :

    if(isCheked) {
getSupportFragmentManager().beginTransaction().show(mFragment).commit();
               
        }
        else {
          getSupportFragmentManager().beginTransaction().hide(mFragment).commit();
        }

Try this out and let me know if it works.

Here's a snippet of the first method in Kotlin...

val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.view?.visibility = View.GONE
dodgy_coder
  • 12,407
  • 10
  • 54
  • 67
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
5

Fragments can be show/hide using transactions.

try {
        FragmentTransaction ft = .getFragmentManager ().beginTransaction ();
        ft.hide (mMapFragment);
    }
    catch (Exception e) {
        e.printStackTrace ();
    }
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
3

The only way works for me :

View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);
Alexey O.
  • 220
  • 2
  • 11
-1

You can wrap the map fragment within a FrameLayout

<FrameLayout
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/theMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>

and then call the animation on the FrameLayout

int valueInPixels = (int) getResources().getDimension(R.dimen.task_list_map_height);
FrameLayout mapLayout = (FrameLayout) findViewById(R.id.mapLayout);
mapLayout.animate().translationY(-valueInPixels).setDuration(600);
marius
  • 637
  • 7
  • 11