[GIS] How to handle two onclick events that not both are fired

arcgis-javascript-apigeoprocessing

I make a web application with ArcGIS API for Javascript (version 3.15) to access a geoprocessing service. The service is executed on an onclick event by the user, because the clicked position is used as an input parameter:

map.on("click", startGPService);

Then the result is displayed and I have an info window enabled, which also opens when clicked, although not specified directly by onclick:

var template = new InfoTemplate();
template.setTitle(getTitleInformation);
resultLayer.setInfoTemplate(template);

The problem is that whenever the user clicks on an info window, also the onclick event of the map is fired and the geoprocessing starts again. It would be great, if the onclick event could recognize, if the info window is clicked or somewhere on the map and only then fire.

Any solution how to deal with that?

This question is linked to this topic: ArcGIS API – Creating Popups for Geoprocessing Result

Best Answer

It's a little unclear what your problem is. I'm assuming that your first click gets some features from a GP service and displays them on the map and your second click (if it's on one of the features) shows the infoWindow for that feature but also re-runs the GP service.

Basically you have two click handlers, one for resultLayer which shows the infoWindow and one for the map which runs the GP service.

Fortunately the map click event object has a graphics property that can tell you if the user has clicked on a feature graphic and just needs to show the infoWindow or if the user has clicked on a part of the map without a feature graphic and thus needs to run the GP service again. Your map.on, instead of looking like this.

map.on("click", startGPService);

Should look like:

map.on("click", function(evt){
   var layerId;
   if (evt.graphic){
      //gets the id of the layer which you can set when you create the layer
      layerId = evt.graphic.getLayer().id
   }
   if(layerId !== "the_id_of_your_layer"){
      startGPService();
   }
});

The Map documentation does not mention this graphic property so it's easy to miss. This is what the documentation has to say about the click event.

"A standard DOM MouseEvent with additional properties mapPoint and screenPoint."

However in the Working with events section in the guide it mentions:

"In addition to the mapPoint and screenPoint properties, the event returned includes a graphic property, which is the esri.Graphic object that received the event."

Related Question