How to differ click and double click on MapWidget

8 views
Skip to first unread message

Malte Legenhausen

unread,
Jan 23, 2009, 4:15:56 AM1/23/09
to Google API Libraries for GWT
Hi,
I readed in the documentation that the click lister will be called
twice on a double click. My first question is why and my second is
there any work around?

Greetz
Malte

Eric Ayers

unread,
Jan 23, 2009, 9:49:57 AM1/23/09
to gwt-goo...@googlegroups.com
I am not sure about the 'why'. searching on the Google-Maps-API group
may reveal some insight.

One workaround would be to delay the click action with a short
duration timer, that is canceled by a subsequent double click event
firing.

some pseudocode:

Timer delayClickTimer;

// code for click
void onClick(MapClickEvent e) {
if (delayClickTimer) { return; }
delayClickTimer = new Timer() {
public void run () {
delayClickTimer = null;
// do single click stuff
}
};
delayClickTimer.schedule(1000); // 1 sec delay
}

// code for double click
void onDoubleClick(MapDoubleClickEvent e) {
if (delayClickTimer) {
delayClickTimer.cancel();
delayClickTimer = null;
}
// do double click stuff
}

Keep in mind that double clicking is bound by default to zooming on
the map, so you will be potentially losing that behavior if you choose
to use double click for one of your application's actions.
--
Eric Z. Ayers - GWT Team - Atlanta, GA USA
http://code.google.com/webtoolkit/

Malte Legenhausen

unread,
Jan 24, 2009, 1:26:19 PM1/24/09
to Google API Libraries for GWT
Thank you Eric.
That is what I want, to use the double click for zooming in and the
single click for adding a marker. I will try this approach and will
give you feedback if its working. Maybe that behavior is a ticket
worth?

Malte Legenhausen

unread,
Jan 24, 2009, 1:57:49 PM1/24/09
to Google API Libraries for GWT
Ok here my solution:

public class MapClickTimerHandler extends Timer implements
MapClickHandler, MapDoubleClickHandler {

private MapClickEvent event;

private boolean delay = false;

@Override
public void run() {
delay = false;
onMapClick(event);
}

public void onDoubleClick(MapDoubleClickEvent event) {
cancel();
delay = false;
onMapDoubleClick(event);
}

public void onClick(MapClickEvent event) {
if (delay) {
return;
}
schedule(200);
delay = true;
this.event = event;
}

public void onMapClick(MapClickEvent event) {
}

public void onMapDoubleClick(MapDoubleClickEvent event) {
}
}

Overwrite the appropriated onMapClick or onMapDoubleClick method.

NOTE:
You have to add this handler as click and doubleclick handler to the
MapWidget!
Reply all
Reply to author
Forward
0 new messages