ArcGIS Javascript识别任务和延迟

时间:2013-06-02 18:21:18

标签: javascript dojo arcgis deferred

我正在尝试在动态地图服务图层上使用识别工具,并显示一个信息窗口,显示附加到记录的图像。我必须四处走动并获得功能层,但这样做可行。我遇到了延期问题。

问题在于:识别任务返回一个dojo延迟对象。我有一个在延迟解决时运行的回调。在该回调函数中,我运行另一个名为queryAttachmentInfos的函数。运行时,“return feature”行将在queryAttachmentInfos函数之前触发。我不知道为什么。回调中的所有内容不应同步发生吗?如何使回调函数等待queryAttachmentInfo完成?我正在使用setTimeout强制脚本等待一秒,这有时会起作用,但我知道这不是一个好的解决方案。

欢迎任何帮助。

以下代码......

function executeIdentifyTask(evt) {
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = map.extent;

        var deferred = identifyTask.execute(identifyParams);

        deferred.addCallback(function(response) {     

          return dojo.map(response, function(result) {
            var feature = result.feature;
            var fLayerPath = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0"
            var featureLayer = new esri.layers.FeatureLayer(fLayerPath);
            var objID = feature.attributes.OBJECTID;
            feature.attributes.layerName = result.layerName;
            //alert(result.layerId);
            if(result.layerName === 'Tax Parcels'){
                featureLayer.queryAttachmentInfos(6737858, function (infos) {
                            if (infos.length>0) {
                                el = document.createElement('img');
                                el.setAttribute('src', infos[0].url);
                                t = document.createElement('table');

                                //first row Request Type
                                r = t.insertRow(0); 
                                r.bgColor="#00FFFF";
                                c = r.insertCell(0);
                                c1 = r.insertCell(1);
                                c.innerHTML="Request Type";
                                c1.innerHTML=feature.attributes.building;

                                //second row District
                                r1 = t.insertRow(-1);
                                c2 = r1.insertCell(0);
                                c2_1 = r1.insertCell(1);
                                c2.innerHTML="District";
                                c2_1.innerHTML=feature.attributes.UNIT;


                                //third row Status
                                r2 = t.insertRow(-1);
                                r2.bgColor="#00FFFF";
                                c3 = r2.insertCell(0);
                                c3_1 = r2.insertCell(1);
                                c3.innerHTML="Status";
                                c3_1.innerHTML=feature.attributes.PARCELID ;

                                var len = infos.length;
                                    for (var i = 0; i < len;i++)
                                    {
                                        newRow = t.insertRow(-1);
                                        newCell = newRow.insertCell(0);
                                        newCell.colSpan=2;
                                        newCell.innerHTML="<a href="+infos[i].url+"/><img src="+infos[i].url+"/>";
                                        //els[i]= document.createElement('img');
                                        //els[i].setAttribute('src', infos[i].url);

                                        //alert(infos[i].url);

                                    }
                                var template = new esri.InfoTemplate("", t);
                                feature.setInfoTemplate(template);
                                //return feature;
                            }
                        else
                        {
                            var template = new esri.InfoTemplate("", "${Postal Address} <br/> Different: ${First Owner Name}");
                            feature.setInfoTemplate(template);
                            //eturn feature;
                        }
                        });
              console.log(feature.attributes.PARCELID);

            }
            else if (result.layerName === 'Building Footprints'){
              var template = new esri.InfoTemplate("", "Parcel ID: ${PARCELID}");
              feature.setInfoTemplate(template);
              //return feature;
            }
            return feature;
          });
        });
        setTimeout(function(){map.infoWindow.setFeatures([ deferred ])},1000); 
        map.infoWindow.show(evt.mapPoint);
      }

2 个答案:

答案 0 :(得分:1)

问题似乎是featureLayer.queryAttachmentInfos()本身是异步的,因此,在“Tax Parcels”的情况下,map.infoWindow.setFeatures()map.infoWindow.show()只能在异步活动完成时调用。

同时,在“建立足迹”的情况下,map.infoWindow.setFeatures()map.infoWindow.show(evt.mapPoint)可以同步调用(在外部异步回调中)。

这意味着应该从两个地方调用一小段代码。您可以重复几行代码或编写工作函数,如下所示:

var showInfoWindow = function(feature, tpl) {
    // A utility function which creates and populates an infowindow
    // and shows it at evt.mapPoint
    feature.setInfoTemplate( new esri.InfoTemplate("", tpl) );
    map.infoWindow.setFeatures(feature);
    map.infoWindow.show(evt.mapPoint);
}

在这里它是在上下文中(为清楚起见,删除了所有庞大的DOM建筑物):

function executeIdentifyTask(evt) {
    identifyParams.geometry = evt.mapPoint;
    identifyParams.mapExtent = map.extent;

    var showInfoWindow = function(feature, tpl) {
        // A utility function which creates and populates an infowindow
        // and shows it at evt.mapPoint
        feature.setInfoTemplate( new esri.InfoTemplate("", tpl) );
        map.infoWindow.setFeatures(feature);
        map.infoWindow.show(evt.mapPoint);
    }

    var deferred = identifyTask.execute(identifyParams);

    deferred.addCallback(function(response) {
        return dojo.map(response, function(result) {
            var feature = result.feature;
            //var objID = feature.attributes.OBJECTID;//???
            feature.attributes.layerName = result.layerName;
            if(result.layerName === 'Tax Parcels') {
                var fLayerPath = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0";
                var featureLayer = new esri.layers.FeatureLayer(fLayerPath);
                featureLayer.queryAttachmentInfos(6737858, function(infos) {
                    var t, tpl;
                    if(infos.length > 0) {
                        t = document.createElement('table');
                        //***** Reinsert several lines of code here *****
                        //***** Remember to localize variables with `var` *****
                        tpl = t;
                    }
                    else {
                        tpl = "${Postal Address} <br/> Different: ${First Owner Name}";
                    }
                    showInfoWindow(feature, tpl);//<<<<< create, populate and display an infowindow
                });
            }
            else if (result.layerName === 'Building Footprints') {
                showInfoWindow(feature, "Parcel ID: ${PARCELID}");//<<<<< create, populate and display an infowindow
            }
            //return feature;//???
        });
    });
}

我所做的就是在没有任何关于道场或弧形的特殊知识的情况下改变周围的事物。除非我出错,否则应该工作。也就是说,我只能测试语法错误,所以准备做一些调试。并记得将笨重的桌子构建线粘贴回来。

答案 1 :(得分:0)

B-B, 我尝试了代码,但我无法使用它。它看起来应该可以工作,或者至少应该在地图上显示一个信息窗口。逐步浏览它显示窗口所需的所有对象。不知道为什么不......

如果我在第一篇文章中不清楚,我道歉。一个问题是识别任务可以(并且通常确实)返回一系列特征。

我看待它的每一种方式,“map.infoWindow.setFeatures([deferred]);”在queryAttachmentInfo触发之前触发行。我有一个愚蠢的解决方法,我使用window.setTimeout函数等待一秒钟,但这并不能保证所有queryAttachmentInfos都将得到解决。