2012-11-27 33 views
1

我要的是,我要开,我使用的值从2角不同的IdentifyTask秒的信息窗口:的Javascript只执行Deferred2,不也推迟

这里是我的代码:

dojo.connect(map, "onClick", executeIdentifyTask); 

identifyTask = new esri.tasks.IdentifyTask("https://server.com/ArcGIS/rest/services/POI_Data/MapServer"); 

identifyTask2 = new esri.tasks.IdentifyTask("https://server.com/ArcGIS/rest/services/ProRail_Data/MapServer"); 

identifyParams = new esri.tasks.IdentifyParameters(); 
identifyParams.tolerance = 15; 
identifyParams.returnGeometry = true; 
identifyParams.layerIds = [0]; 
identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL; 
identifyParams.width = map.width; 
identifyParams.height = map.height; 

identifyParams2 = new esri.tasks.IdentifyParameters(); 
identifyParams2.tolerance = 10; 
identifyParams2.returnGeometry = true; 
identifyParams.layerIds = [23, 26, 31, 33]; 
identifyParams2.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL; 
identifyParams2.width = map.width; 
identifyParams2.height = map.height; 

function executeIdentifyTask(evt) { 

    identifyParams.geometry = evt.mapPoint; 
    identifyParams.mapExtent = map.extent; 
    var deferred = identifyTask.execute(identifyParams); 
    identifyParams2.geometry = evt.mapPoint; 
    identifyParams2.mapExtent = map.extent; 

    var deferred2 = identifyTask2.execute(identifyParams2); 

    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; 
      feature.attributes.layerName = result.layerName; 
      if (result.layerName === 'Spoortoegang') { 
       console.log(feature.attributes.PARCELID); 
       var template = new esri.InfoTemplate("ToegangsNummer ${ToegangsNr}", "Toegangsnummer ${ToegangsNr} <br/> Baanvak: ${Baanvak}, <br/> Geocode: ${Geocode}, <br/> Kilometer: ${Kilometrering}"); 
       feature.setInfoTemplate(template); 
       alert("Spoortoegang"); 
      } 
      else if (result.layerName === 'sein') { 
       var template = new esri.InfoTemplate("", "Parcel ID: ${NUMMER}"); 
       feature.setInfoTemplate(template); 
      } 
      return feature; 
     }); 
    }); 

    deferred2.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; 
      feature.attributes.layerName = result.layerName; 
      if (result.layerName === 'Wissels') { 
       console.log(feature.attributes.PARCELID); 
       var template = new esri.InfoTemplate("Wissel", "Toegangsnummer ${ToegangsNr} <br/> Baanvak: ${Baanvak}, <br/> Geocode: ${Geocode}, <br/> Kilometer: ${Kilometrering}"); 
       feature.setInfoTemplate(template); 
       alert("Wissels"); 

      } 
      else if (result.layerName === 'sein') { 
       var template = new esri.InfoTemplate("", "Sein Nummer: ${NUMMER}"); 
       feature.setInfoTemplate(template); 
       alert("sein"); 
      } 
      else if (result.layerName === 'Sporen') { 
       console.log(feature.attributes.PARCELID); 
       var template = new esri.InfoTemplate("Wissel", "Toegangsnummer ${ToegangsNr} <br/> Baanvak: ${Baanvak}, <br/> Geocode: ${Geocode}, <br/> Kilometer: ${Kilometrering}"); 
       feature.setInfoTemplate(template); 
       alert("Sporen"); 
      } 
      else if (result.layerName === 'bovenleidingpaal') { 
       console.log(feature.attributes.PARCELID); 
       var template = new esri.InfoTemplate("Wissel", "Toegangsnummer ${ToegangsNr} <br/> Baanvak: ${Baanvak}, <br/> Geocode: ${Geocode}, <br/> Kilometer: ${Kilometrering}"); 
       feature.setInfoTemplate(template); 
       alert("bovenleidingspaal"); 
      } 

      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.resize(195, 75); 
    map.infoWindow.setFeatures([deferred]); 
    map.infoWindow.show(evt.mapPoint); 

    map.infoWindow.resize(195, 75); 
    map.infoWindow.setFeatures([deferred2]); 
    map.infoWindow.show(evt.mapPoint); 
} 

我仅从deferred2得到警报和infowindow内容,而不是deferred

+0

什么是你'result.layerName'?尝试提醒'if(result.layerName ==='Spoortoegang'){'in'deferred' – Cerbrus

+0

你没有得到正确的问题。我收到了警报。只有不从延期..所以我从Wissels得到警报.. result.layername获取,如果LayerId [23,26,31,33];他们中的任何一个都是'Wissels'..只有我没有得到LayerID [0];从延期。所以我没有得到resylt.layerName ==='Spoortoegang'。这是否更清楚? –

+0

我混合了2个延期的,是的。我想你没有得到到任何'if'的在'deferred' – Cerbrus

回答

1

任何你可以缩短代码deferred2

deferred2.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; 
     feature.attributes.layerName = result.layerName; 
     var template; 
     switch(result.layerName){ 
      case 'Wissels': 
      case 'Sporen': 
      case 'bovenleidingpaal': 
       template = new esri.InfoTemplate("Wissel", "Toegangsnummer ${ToegangsNr} <br/> Baanvak: ${Baanvak}, <br/> Geocode: ${Geocode}, <br/> Kilometer: ${Kilometrering}"); 
      break; 
      case 'sein': 
       var template = new esri.InfoTemplate("", "Sein Nummer: ${NUMMER}"); 
      break; 
     } 
     console.log(feature.attributes.PARCELID); 
     feature.setInfoTemplate(template); 
     alert(result.layerName); 

     return feature; 
    }); 
}); 

这应该是相同的。

+0

我明白你的意思从我的代码是的,但我想对于Wissels不同的弹出窗口,我只是复制从Spoortoegang的值,因为我想看看它做什么,在那之后我想改变的值。但你有什么想法如何解决它的deferred.addCallback被调用,不仅deferred2 .... –

+0

啊,这是有道理的,是的。让它工作,然后让它返回正确的值,对吧? 从我所看到的,除了具有不同要求的'if-else'块之外,'deferred'函数都是相同的。你是否在'if'-block之前的'deferred'内尝试'alert'?我想'if'的条件没有返回TRUE; – Cerbrus

+0

警报心不是如果之前调用,也不是如果 –