Monday, July 16, 2012

how to implement double click listener for Google maps in Android

here is the code for implementing onDoubleTap listener in your MapActivity.

note mapview.setOnTouchListener() method and return true in onDown method.

the code:
 
public class MYMapActivity extends MapActivity implements  
GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
GestureDetector gestureDetector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mymapview);
        //get the MapView
        MapView mapView = (MapView) findViewById(R.id.mapview);
        // enable zoom controls in the map view 
        mapView.setBuiltInZoomControls(true);

        gestureDetector = new GestureDetector(this, this);
        //register the double tap listener to this object
        gestureDetector.setOnDoubleTapListener(this);
        mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });

    }
@Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent mev) {
        System.out.println("******onDoubleTap*******");
        //do what ever you want here
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        System.out.println("******onDown*******"); 
        //must return true so that onDoubleTap method fires
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, 
           float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, 
           float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

2 comments:

  1. A very helpful post. The "return true" in onDown() method in particular was the thing that helped me. it drove me crazy why double tap method was never invoked.

    ReplyDelete
  2. Well, actually, "return true" for onDown() is not needed. I seems to running fine without that. But there are issues with this approach. While double clicking if a drag occurs, application fails to proceed. Extending MapView and overriding the onTouch() of Mapview in the overriding class is a better solution.

    ReplyDelete