How do I respond to a tap on an Android MapView, but ignore pinch-zoom?

后端 未结 1 1283
自闭症患者
自闭症患者 2020-12-13 22:56

I have a MapView in an activity and it works fine, the map shows, and it responds to taps, and I can extract the location easily. However this handler is also responding to

相关标签:
1条回答
  • 2020-12-13 23:10

    After much head-scratching and trying various approaches, this one is working well so far. The code follows the motion events. When we get an ACTION_DOWN event, it marks the isPinch flag as false (we don't know if it's a pinch or not yet), but as soon as we get a touch event (i.e. ACTION_MOVE) involving two fingers, isPinch is set to true, and so when the onTap() event fires, it can see if there was a pinch or not.

    class MapOverlay extends com.google.android.maps.Overlay
    {
    private boolean   isPinch  =  false;
    
    @Override
    public boolean onTap(GeoPoint p, MapView map){
        if ( isPinch ){
            return false;
        }else{
            Log.i(TAG,"TAP!");
            if ( p!=null ){
                handleGeoPoint(p);
                return true;            // We handled the tap
            }else{
                return false;           // Null GeoPoint
            }
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView)
    {
        int fingers = e.getPointerCount();
        if( e.getAction()==MotionEvent.ACTION_DOWN ){
            isPinch=false;  // Touch DOWN, don't know if it's a pinch yet
        }
        if( e.getAction()==MotionEvent.ACTION_MOVE && fingers==2 ){
            isPinch=true;   // Two fingers, def a pinch
        }
        return super.onTouchEvent(e,mapView);
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题