Search code examples
javascriptmapsesri

Map onclick event taking precedence over graphic click event


I've a click event registered on the map which shows a infowindow. But I also have a click event registered on the map.graphics layer which will show the graphic's infowindow.

The problem is that when I click on the graphic, its infowindow is shown for a split second and then it is overriden by the map's infowindow.

How can I avoid firing the map's onclick event when a graphic is clicked?

//map's onclick event
map.on("click", function(event){
   .
   .
   .
   map.infoWindow.show(event.mapPoint);
});

// graphic's onclick event
dojo.connect(map.graphics, "onClick", function(e) {
    var html = e.graphic.infoTemplate;
    map.infoWindow.setTitle("Address");
    map.infoWindow.setContent(html);                                                        
    map.infoWindow.resize('300', '120');
    map.infoWindow.show(slcGraphic.geometry); // slcGraphic is a global variable
});

Solution

  • I figured it out. The map's click handler has a property event.graphic which will not be null if clicked on a graphic. So I modified my map's click handler as follows and removed the graphic's click handler altogether.

    map.on("click", function(event){
       if(event.graphic)
       {
          // do what needs to happen on marker click
       }
       else
       {
          // do what needs to happen on map click
       }
    });