Open fullscreen SupportMapFragment on Double-Tap

983 Views Asked by At

I'm using the Google Maps Android API v2 and I want to be able to open as fullscreen a small map (= SupportMapFragment in my view) on tap (or double tap).

Is this technically possible? If yes, how to achieve it?

Thanks in advance.

1

There are 1 best solutions below

0
On

Yes, that is definitely possible.

You could for example have a Button, and upon pressing the Button the MapFragment / SupportMapFragment will be added to a container layout inside your Activity's layout file.

Inside the onClick method of your Button you add the Fragment to the container layout:

YourMapFragment f = new YourMapFragment();
getFragmentManager().beginTransaction().add(R.id.container_layout, f).commit();

In this case, I would recommend that "container_layout" is an empty FrameLayout, used as a placeholder in your Activity's layout file. This is where the Fragment will then appear.

If you really want to use Taps, this is how you can recognize for example a double-tap:

  • You will need an interface that the class that needs to recognize the gesture has to implement
  • You need a custom TouchManager, that will use callbacks to the interface to interpret the gesture

The interface:

    public interface GestureInterface {    
        /**
         * returns the recognized gesture from the touchmanager
         * and enables the user of the interface to react to the gesture (or not)
         * @param gesture e.g. TouchManager.SWIPE_LEFT
         */
        public void onGestureRecognized(int gesture);
    }

The TouchManager:

public class TouchManager extends GestureDetector.SimpleOnGestureListener {

    public static final int         DOWN                    = 1;
    public static final int         DOUBLE_TAP              = 2;

    /** the class that initialized the gesture-recognizer and will be notified upon gestures made by the user */
    private GestureInterface        caller;

    /**
     * constructor
     * @param the caller that implements the gestureinterface
     */
    public TouchManager(GestureInterface caller) {
        this.caller = caller;
    }

    /**
     * you need this shit to return true, otherwise gestures wont work
     */
    @Override
    public boolean onDown(MotionEvent e) {
        caller.onGestureRecognized(DOWN);
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        caller.onGestureRecognized(DOUBLE_TAP); // callback
        return true;
    }
}

And inside your Activity (or Customview or wherever you want to recognize the gesture): (In this case tapping on the Activity will call the Touchmanager.

public class YourActivity extends Activity implements GestureInterface {

private GestureDetector gd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.yourlayout);

        // initialize the touch manager
        gd = new GestureDetector(this, new TouchManager(this));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }

    @Override
    public void onGestureRecognized(int gesture) {

        // react to callbacks
        switch (gesture) {
        case TouchManager.DOUBLE_TAP:
            // do something

            YourMapFragment f = new YourMapFragment();
            getFragmentManager().beginTransaction().add(R.id.container_layout, f).commit();
            break;
        }
    } 
}

Wherever you want to recognize the gesture, you return GestureDetector.onTouchEvent(...).