0

我对平移和双击谷歌地图有不同的实现。我在这个链接的帮助下实现了平移功能。

但问题是当用户双击地图时,地图上的更新会发生两次,因为 action_up 事件被触发了两次。

我的要求是不要在双击时做任何事情以及一点点地图也应该作为地图的平移(在 Action_up 事件中更新地图的原因)。

4

1 回答 1

0

Your question is kind of unclear but I think what you want is to prevent double clicks from updating your map twice.

Using the code you provided (from the link), to prevent double clicks to update the map twice you can use this code:

public  class TouchableWrapper extends FrameLayout {

    // Map updates only when click has been done 250 Milliseconds  after the last one
    private long lastClicked;
    private static final long CLICK_DELAY = 250L;

    private long lastTouched = 0;
    private static final long SCROLL_TIME = 200L; // 200 Milliseconds, but you can adjust that to your liking
    private UpdateMapAfterUserInterection updateMapAfterUserInterection;

    public TouchableWrapper(Context context) {
        super(context);
        // Force the host activity to implement the UpdateMapAfterUserInterection Interface
        try {
            updateMapAfterUserInterection = (ActivityMapv2) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement UpdateMapAfterUserInterection");
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastTouched = SystemClock.uptimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                final long now = SystemClock.uptimeMillis();
                if (now - lastTouched > SCROLL_TIME) {
                    if (lastClicked == 0 || ( now - lastClicked  > CLICK_DELAY)) {
                        // Update the map
                        updateMapAfterUserInterection.onUpdateMapAfterUserInterection();
                        lastClicked = now;
                    }
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    // Map Activity must implement this interface
    public interface UpdateMapAfterUserInterection {
        public void onUpdateMapAfterUserInterection();
    }

}

Explanation:

This saves the time of last UP motion. Then when the next one happens it checks if 250 Milliseconds have passed, if not the update is skipped.

If however you want to prevent double clicks from updating your map at all, you will need to delay the whole update in a specific amount of time. During that time you check for any additional clicks, if they happen, you cancel the update of the map.

于 2014-08-13T11:19:10.650 回答