如何响应Android MapView上的点击,但忽略双指缩放?

时间:2011-01-26 15:05:44

标签: android

我在一个活动中有一个MapView,它工作正常,地图显示,它响应水龙头,我可以轻松提取位置。然而,这个处理程序也响应了捏拉缩放,所以如果用户试图捏缩放,应用程序会响应,就像他们点击一样(这对他们来说非常混乱)。

我如何响应MapView上的点击并只选择单击,特别是忽略双指缩放和双击?

我是否需要使用OnTouchEvent()而不是OnTap()?如果是这样,我如何区分不同的触摸事件,以及如何访问GeoPoint?

以下是MapActivity内部的代码:

class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean onTap(GeoPoint p, MapView map)
    {
        if ( p!=null )
        {
            // Do stuff with the geopoint
            return true;                                 // We handled the tap
        }
        else
        {
            return false;                                // We didn't handle the tap
        }
    }
}

1 个答案:

答案 0 :(得分:27)

经过多次头脑风暴和尝试各种方法后,这个方法到目前为止运作良好。代码遵循运动事件。当我们得到一个ACTION_DOWN事件时,它将isPinch标志标记为false(我们不知道它是否是一个夹点),但是一旦我们得到一个涉及两个手指的触摸事件(即ACTION_MOVE),isPinch就会被设置为是的,所以当onTap()事件触发时,它可以看到是否有夹点。

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);
}

}