2013-06-02 29 views
0

我試圖在動態地圖服務圖層上使用識別工具,並顯示一個信息窗口,顯示附加到記錄的圖像。我不得不四處尋找並獲取要素圖層,但這很好。我遇到延期問題。ArcGIS Javascript識別任務和延期

下面是問題:標識任務返回一個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); 
     } 
+0

」回調中的所有事情不應該同步發生嗎?「不必要。異步回調本身可能包含異步操作。 –

回答

1

這個問題似乎是featureLayer.queryAttachmentInfos()本身是異步的,在「稅收包裹」,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;//??? 
     }); 
    }); 
} 

所有我我們所做的就是在沒有任何關於dojo或arcgis的特定知識的情況下,對所有東西進行洗牌。除了我的錯誤,一切應該工作。也就是說,我只能測試語法錯誤,因此準備做一些調試。並記得粘貼龐大的建造線回來。

+0

你是對的,queryAttachmentInfos是延遲的。我沒有明白。我不認爲你的解決方案會起作用,因爲showInfoWindow必須在回調函數之外被調用。標識任務可以返回一個值數組(請參閱「return dojo.map」一行)。 – bdwyer

+0

我想我正在處理嵌套延期。外面的一個(延期的。回調)和內部的一個(queryAttachmentInfo)。有沒有辦法讓外層的人等待內層? – bdwyer

+0

bd,你是說你*認爲*我的代碼不起作用或它不起作用?如果你運行它會發生什麼?沒有明顯的範圍問題應該防止'showInfoWindow'工作 - 'feature'和'tpl'被傳遞給它,其他所有東西('esri','map'和'evt')應該是自動可用的,除非你的原始代碼在這方面是不正確的。據我所知,扭轉內部和外部延遲並沒有什麼好處。如果'identifyTask'可以返回一個值的數組,那麼這是另一個問題,不能從原始問題預測。 –

0

B-B, 我試過了代碼,我無法得到它的工作。它看起來應該起作用,或者至少應該在地圖上顯示infowindow。逐步完成它需要顯示窗口的所有對象。不知道爲什麼它不....

我道歉,如果我不清楚我的第一篇文章。一個問題是識別任務可以(並且通常)返回一系列特徵。

每一個我看它的方式,「map.infoWindow.setFeatures([deferred]);」在queryAttachmentInfo觸發之前發生一連串的火災。我有一個愚蠢的解決方法,我使用window.setTimeout函數等待一秒鐘,但這並不能保證所有的queryAttachmentInfos將被解析。 「