I wrote following code to show GIS attributes, whenever user clicks on the map. But IdentifyTask - complete event is being called multiple times. I am using ArcGIS JS API 3.9. I am able to see from the fiddler that REST request is fired only once and able to retrieve multiple features attributes. Sometimes, I have seen it is being called single time only even attributes are multiple. How to debug on this inconsistent behavior?
vMap.on('click', function(event){
var vIdentifyTask,
vIdentifyParams,
vIdentifyTaskDeferred;
vIdentifyTask = new IdentifyTask(url),
vIdentifyParams = new IdentifyParameters();
vIdentifyParams.tolerance = 3;
vIdentifyParams.returnGeometry = true;
vIdentifyParams.layerIds = [3,4];
vIdentifyParams.width = vMap.width;
vIdentifyParams.height = vMap.height;
vIdentifyParams.geometry = event.mapPoint;
vIdentifyParams.mapExtent = vMap.extent;
vIdentifyTask.on('complete', showAttributesPopup);
vIdentifyTask.on('error', showError);
vIdentifyTask.execute(vIdentifyParams);
function showAttributesPopup(featureResults) {
//this function is being called multiple times
}
function showError() {
}
});
Bhavin,
Are you adding this line inside a loop?
vMap.on('click', function(event){
No, it was not in loop but probably my code was not de-registering 'click' event.
e.g. I have two map toolbar buttons. One is to draw the line on the map and another is to see the GIS attributes. At a time one button action will remain active. So, in my case, I click on buttonA and register the 'click' event. Then if I click on buttonB and register the 'click' event again but previously registered 'click' event was not getting removed automatically.
To fix it, I am making sure that whenever new events are getting registered, other events have been removed.
Hope this will help.
Bhavin,
That should help. Most of the time I use a var to hold the on event that way I can call remove() on that var.
var mcEvt = on(vMap, 'click', function(event){ .....
mcEvt.remove();
Yes, I did the same to fix the issue.