Select to view content in your preferred language

Deferreds and Identify Task

6317
17
05-28-2013 06:28 AM
BrendanDwyer
Frequent Contributor
Asynchronous programming newbie question.  I need to run an identify task on a dynamic map service layer.  After I get the results of the identify, I need to do another operation before the results are sent to the popup.  I'll use that operation to format the infoTemplate.

How do I prevent the popup from displaying until I format it?  It seems that as soon as the result comes back, the dojo will display the popup with the returned records.  I think I need to chain the deferreds or create a deferred list, but I can't grok how it works.


function executeIdentifyTask(evt) {
identifyParams.geometry = evt.mapPoint;
identifyParams.mapExtent = map.extent;
      
var deferred = identifyTask.execute(identifyParams);
       deferred.addCallback(function(response) {    
          // response is an array of identify result objects   
          // Let's return an array of features.
       return dojo.map(response, function(result) {
       var feature = result.feature;
       if(result.layerName === 'Tax Parcels'){
if (blah.length>0) {
  FORMAT CONTENT HERE
          var template = new esri.InfoTemplate("", "${Postal Address} <br/> Owner of record: ${First Owner Name}");
          feature.setInfoTemplate(template);
}
else
{
  FORMAT CONTENT DIFFERENT WAY HERE
          var template = new esri.InfoTemplate("", "${Different Stuff} <br/> Owner of record: ${Blah}");
          feature.setInfoTemplate(template);
}
});
        console.log(feature.attributes.PARCELID);
       }
       else if (result.layerName === 'Building Footprints'){
        var template = new esri.InfoTemplate("", "Parcel ID: ${PARCELID}");
         feature.setInfoTemplate(template);
        }
        return feature;
        });
     });

     
        // InfoWindow expects an array of features from each deferred
        // object that you pass. If the response from the task execution
        // above is not an array of features, then you need to add a callback
        // like the one above to post-process the response and return an
        // array of features.
        map.infoWindow.setFeatures([ deferred ]);
        map.infoWindow.show(evt.mapPoint);
      }
0 Kudos
17 Replies
KenBuja
MVP Esteemed Contributor
Here's an example of using a DeferredList to await for the results of two different IdentifyTasks.
0 Kudos
BrendanDwyer
Frequent Contributor
Here's an example of using a DeferredList to await for the results of two different IdentifyTasks.


Ken, thanks for the reply but I don't think it applies to my situation.  I need to do the following:

Set off an identify task
For each record that comes back (in the result array), get the objectID and the layer id.
Create a feature layer (concatenating a set url string with the layer id)
Run the queryAttachemntInfos function on the feature layer and the objectID
Configure the infoTemplate for each record based on what comes back from queryAttachementInfos.

So I think I need nested deferreds.  I attached a txt file to this post.  You need to change the extension from .txt to .html.  Where its going wrong is in the executeIdentifyTask function.  I'm trying to nest the deferreds but it doesn't look like I'm setting up the callback right.  Any ideas?
0 Kudos
KellyHutchins
Esri Frequent Contributor
One option might be to just add the layers you want to query to your application as feature layers in Selection only mode. When you add each feature layer define the info template for that layer.  Note that in this example we don't have to query for attachment info because we've set showAttachments to true when defining the info template for the feature layer that has attachments. Then when you click the map you can use the feature layer's selectFeatures method to select the features of interest and display them in the popup.


Here's a jsfiddle that shows this:

http://jsfiddle.net/Wav9r/
0 Kudos
RobertKirkwood
Frequent Contributor

Kelly,

I am using the deferred method for identify task. i have two services i would like to create tasks for and use info templates for each sub layer. The problem i am having is that it will only identify the last service in the code. in my main application it will identify the 1st layer i turn on that is in the first service. however, when i turn on layers in the second service it will no longer identify layers in the first service.

I created jsfiddle that is a simplified version of my application. Do you have any idea what i am doing wrong here?

Also, I am using this method so i can identify grids/rasters...

Edit fiddle - JSFiddle

0 Kudos
KellyHutchins
Esri Frequent Contributor

Robert,

There's only one info window so whichever identify finishes last will overwrite the first one with the content. I think instead you'll want to use dojo/promises/all to determine when both identify tasks have finished and then set the info window content. We have a sample in the help that shows how to work with multiple deferreds and dojo/promises/all here:

Manage results from multiple queries | ArcGIS API for JavaScript

RobertKirkwood
Frequent Contributor

Thanks. I will check out that example and let you know if i get it figured out. Thanks!

0 Kudos
RobertKirkwood
Frequent Contributor

Kelly,

Quick question:

Do i keep the identify tasks set up the way i have them and then use the promise?

0 Kudos
KellyHutchins
Esri Frequent Contributor

Here's a fiddle that shows one way to accomplish this:

Edit fiddle - JSFiddle

RobertKirkwood
Frequent Contributor

Kelly,

Thanks so much. This is perfect. once again you have help me get over a major frustrating wall!

0 Kudos