simplegeo / polymaps

Polymaps is a free JavaScript library for making dynamic, interactive maps in modern web browsers.
http://polymaps.org/
Other
1.6k stars 213 forks source link

Any way to stopPropagation() of onclick event when moving map? #117

Closed RyanEwen closed 12 years ago

RyanEwen commented 12 years ago

Hi there,

I would like to listen for click events on my map but I am unsure as of how to prevent the map move event from triggering a click event once a map drag has finished.

Is there a way to do that?

shawnbot commented 12 years ago

You can stop the map from dragging by calling preventDefault() on "mousedown" events.

RyanEwen commented 12 years ago

Sorry, I think you misunderstood.

I don't want to prevent map drags. The issue is that when the map is dragged it triggers a 'click' event when dragging is ended. I can't differentiate between this click event and an actual click (without drag) event.

In my specific case I would like to deselect an item (something I've added on my own custom layer) when the map is clicked. However, simply dragging the map causes items to become deselected as well, which isn't desired.

shawnbot commented 12 years ago

Ah, sorry! Yeah, I think that in order to prevent them you can either listen for "click" events before adding the drag handler (so that your listener gets called first and you can stopPropagation()) or listen for "mouseup" events and preventDefault() on those.

RyanEwen commented 12 years ago

Thanks for the suggestions. I tried both but had no luck. I think it's because if I stopPropagation() on a click, it's not doing anything aside from stopping clicks from reaching the map's container (which I am already doing).

When I do preventDefault() on mouseup then it prevents the legit map clicks.

Sad face. Oh well.

shawnbot commented 12 years ago

Okay, how about this technique:

  1. on "mousedown", remember the time in milliseconds (var downtime = +new Date())
  2. on "mouseup", also remember the time (var uptime = +new Date())
  3. on "click", check the delta (var delta = uptime - downtime), and if it's >= some threshold (say, 250ms), preventDefault().

I used this on Surging Seas to prevent errant clicks on place labels, and it works pretty well.