Robban
Robban

Reputation: 1221

Bing map AJAX Control V7 - How does one detect clicks but not drags?

I have a Bing map on my web page, and I want to detect when the user clicks in the window. However, I do not wish to detect when the user drags the map (this also generates a "click" event) . What is the best way to get only "pure" click events?

Upvotes: 0

Views: 1030

Answers (2)

tanguy_k
tanguy_k

Reputation: 12293

Very simple:

Microsoft.Maps.Events.addHandler(map, 'click', onClick);

function onClick(e) {
    if (e.mouseMoved === false && e.isPrimary === true) {
        // Left click not being a drag
        ...
    }
}

mouseMoved is true with a drag and drop and false otherwise.

The MouseEventArgs documentation http://msdn.microsoft.com/en-us/library/gg406731.aspx does not reference mouseMoved :/

Upvotes: 1

Robban
Robban

Reputation: 1221

My solution ended up beeing a manual check if the click position was close to the position where the mouse was pushed down.

Microsoft.Maps.Events.addHandler(map, "click", clickHandler);
Microsoft.Maps.Events.addHandler(map, "mousedown", function(me) { lastMouseDownPoint = new Microsoft.Maps.Point(me.getX(), me.getY());});


function clickHandler(mouseEventArgs){
  var point = new Microsoft.Maps.Point(mouseEventArgs.getX(), mouseEventArgs.getY());

  //Drag detection

  // Edited since the comma is incorrect, should be a plus as per pythagorean theorem
  var dist = Math.sqrt(Math.pow(point.x-lastMouseDownPoint.x,2) + Math.pow(point.y-lastMouseDownPoint.y,2));
  if(dist > 5) {
    // We call this a drag
    return;
  }

// We have a "pure" click and can process it

}

Upvotes: 3

Related Questions