title: Google Maps API event listener priority
body: <p>I have a map which has a <code>google.maps.event.addListener</code> set on the <code>map</code> object for <code>click</code> events to place a marker on the map.  Now I want to add a context menu to the markers, so am creating a custom overlay in the <code>floatPane</code>. Again  <code>google.maps.event.addListener</code> is used to add a <code>rightclick</code> event to each marker to position and display the menu.</p>
  
  <p>Once the menu is displayed I want it to be cleared by either a menu item being selected, escape being pressed, or a click on the map.  </p>
  
  <p>The menu has a <code>div</code> for each items using jQuery <code>.on</code> to attach a <code>click</code> handler to them, whilst when the menu is displayed a <code>keydown</code> handler is attached to the <code>document</code> using <code>.on</code> to check for escape being pressed.  These work as desired but I am unable to find a satisfactory solution to detecting a click on the map.</p>
  
  <p>If I use <code>google.maps.event.addListener</code> on the <code>map</code> object to cancel the menu it works, but also registers with the listener to add a marker and <code>event.stopPropagation()</code> does not affect this.  <em>i.e.</em> Cancelling the menu also adds a marker.  I believe the Google API triggers the handlers in the order they were added with no way to change the priority.</p>
  
  <p>I have also tried using a jQuery <code>.on</code> handler on the <code>div</code> to which the map is attached.  But if I use <code>click</code> or <code>mouseup</code> event it is triggered by the right click which adds the menu. <em>i.e.</em> The menu flashes on screen as it appears but immediately closes.  A <code>mousedown</code> event avoids this problem, but obviously still triggers the Google API handler to add a marker.  It also registers the click to drag the map to scroll it, which the Google API does not and would be the preferred behaviour.</p>
  
  <p>So it seems to me there are only two solutions to this problem.</p>
  
  <p>One would be to cancel any handlers on the map when the menu is displayed, then re-add them once it closes.  This seems unnecessarily excessive though.</p>
  
  <p>The other is to make wrap the content of the handlers in a conditional statement to detect whether the menu is open.  This could be either by using a global variable as a flag or adding a status property to the menu's object.</p>
  
  <p>But this would make the code less reusable and go against the point of having separate event handlers.  I may as well just put the code to close the menu in the original handler too.</p>
  
  <p>Is there anything I am missing?  It does not seem to be too obscure a thing to want to do but the inability to set the priority of handlers in the Google API means either having to code the handlers around each other.</p>