我正在尝试在动态地图服务图层上使用识别工具并显示一个显示附加到记录的图像的信息窗口。我必须四处寻找并获取要素层,但这没问题。我遇到了延期的问题。
这就是问题所在:identify 任务返回一个dojo 延迟对象。当延迟解决时,我有一个回调。在该回调函数中,我运行另一个名为 queryAttachmentInfos 的函数。运行时,“返回功能”行将在 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);
}