Select to view content in your preferred language

Query multiple services

1333
8
01-17-2011 04:20 AM
LemvigKommune
Deactivated User
Hi

I'm trying to query multiple services and pupulate the result into a combobox, i've got it to do it on one layer, but if i as shown below makes to layers in the xml-file, it only query the last one.

<layers>
  <layer>
   <layername>Lokalplaner</layername>
   <layerurl>http://gis.rksk.dk/ArcGIS/rest/services/Temaer/MapServer/3</layerurl>
   <layeroutfield>PLANNAVN</layeroutfield>   
  </layer>
  <layer>
   <layername>Adresser</layername>
   <layerurl>http://gis.rksk.dk/ArcGIS/rest/services/AdresseLokalplan/MapServer/0</layerurl>
   <layeroutfield>ADR_NR_POST</layeroutfield>   
  </layer> 
 </layers>


the code for the query-loop is here...

var lyrList:XMLList = configXML..layer;
    for (var i:int = 0; i < lyrList.length(); i++){
     layerURL = lyrList.layerurl;
     layerOutField = lyrList.layeroutfield;
     layerZoomScale = lyrList.layerzoomscale;    
     var comboQueryTask:QueryTask = new QueryTask();
     comboQueryTask.url = layerURL;
     comboQueryTask.useAMF = false;
     var comboQuery:Query = new Query();
     comboQuery.returnGeometry = true;
     comboQuery.where = "1=1";
     comboQuery.outFields = [layerOutField];
     comboQuery.outSpatialReference = map.spatialReference;
     comboQueryTask.execute(comboQuery);     
     comboQueryTask.addEventListener(QueryEvent.EXECUTE_COMPLETE, onQueryComplete);
     
    }
//    comboQueryTask.addEventListener(QueryEvent.EXECUTE_COMPLETE, onQueryComplete);
    var dataSortField:SortField = new SortField();
    dataSortField.name = "label";  //this must be a field returned in from esri:Query
    var dataSort:Sort = new Sort();
    dataSort.fields = [dataSortField];
    results.sort = dataSort;
    results.refresh(); 
    acAdv.dataProvider = results;


   }
   
   private var commExtent:Extent = new Extent();
   private function onQueryComplete(event:QueryEvent):void
   {
    var featureSet:FeatureSet = event.featureSet;
    for each (var graphic:Graphic in featureSet.features)
    {
     if (graphic.attributes[layerOutField] != null){
      var fieldValue:String = graphic.attributes[layerOutField].toString();
      results.addItem({label: fieldValue, data: graphic, zoom: layerZoomScale}); 
     }
     
    }   
    var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(featureSet.features);
    commExtent = graphicsExtent;    
   }


does anybody have any idea how to solve this ?

see arcGIS.com for the widget in use

Thx in advance...

Mads
Tags (2)
0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus
Mads,

  I have not tested it but just looking at your code I see that you are not give the query task both of the urls only one. So cahnge this line and see if that is your issue:

comboQueryTask.url = lyrList.layerURL;
0 Kudos
LemvigKommune
Deactivated User
thanks Robert, but that was'nt it, it changes the layerURL in the first line after the for statement - when i debug, i can see the layerURL change, as does the layeroutfield...

i just can't get it to do the query until it has iteratet through it all...

mabye i should try to run the query after widgetComplete, just to see if that will do it.

Mads
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Mads,

   I am not real sure if this has it or not, but this seems to be working for me.

   //this function called when the widget's configuration is loaded
   private function init():void
   {
    if (configXML) // checking for valid content in the configuration file
    {
     promptString = configXML.promptlabel || "SuperSearch";
     matchTypeString = configXML.matchtype || "anyPart";
     styleNameString = configXML.stylename || "underline";
     dropDownRows = configXML.dropdownrows || 10;
     dropDownListWidth = configXML.dropdownlistwidth || 250;
     checkSearchResult = configXML.checksearchresult;
    }
    
    // setting the properties of the advancedAutocompleteDropdown
    acAdv.prompt = promptString;
    acAdv.matchType = matchTypeString;
    acAdv.dropDownRowCount = dropDownRows;
    // setting the dropdownwidth to a minimum of 250
    if (dropDownListWidth < 250){
     acAdv.dropDownWidth = 250;
    }
    else{
     acAdv.dropDownWidth = dropDownListWidth;
    }
    // setting the dropdownrowcount to a max of 10
    if (dropDownRows > 10){
     acAdv.dropDownRowCount = 10;
    }
    else{
     acAdv.dropDownRowCount = dropDownRows;
    }
    
    var lyrList:XMLList = configXML..layer;
    for (var i:int = 0; i < lyrList.length(); i++)
    {
     layerURL = lyrList.layerurl;
     layerOutField = lyrList.layeroutfield;
     layerZoomScale = lyrList.layerzoomscale;    
     var comboQueryTask:QueryTask = new QueryTask();
     comboQueryTask.url = layerURL;
     comboQueryTask.useAMF = false;
     var comboQuery:Query = new Query();
     comboQuery.returnGeometry = true;
     comboQuery.where = "1=1";
     comboQuery.outFields = [layerOutField];
     comboQuery.outSpatialReference = map.spatialReference;
     comboQueryTask.execute(comboQuery, new AsyncResponder(onQueryComplete,onFault,layerOutField));
     function onQueryComplete(featureSet:FeatureSet, token:String = null):void 
     {
      for each (var graphic:Graphic in featureSet.features)
      {
       var fieldValue:String = graphic.attributes[token].toString();
       results.addItem({label: fieldValue, data: graphic});
      }   
      var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(featureSet.features);
      commExtent = graphicsExtent;    
     }
     //on fault
     function onFault(info:Object, token:Object = null):void
     {
      trace(info.toString());
     }
    }
    acAdv.dataProvider = results;

// 12-01-2011 adding graphics layer 
    
    // markersymbol
    const resultMarkerSymbolStyle:String = configXML.symbols.point.style || 'diamond';
    const resultMarkerSymbolSize:Number = configXML.symbols.point.size || 15;
    const resultMarkerSymbolColor:uint = configXML.symbols.point.color || 0xFF0000;
    const resultMarkerSymbolAlpha:Number = configXML.symbols.point.alpha || 0.5;
    resultMarkerSymbol = new SimpleMarkerSymbol(resultMarkerSymbolStyle, resultMarkerSymbolSize, resultMarkerSymbolColor, resultMarkerSymbolAlpha);
    
    // linesymbol
    const resultLineSymbolStyle:String = configXML.symbols.line.style || 'solid';
    const resultLineSymbolWidth:Number = configXML.symbols.line.width || 2;
    const resultLineSymbolColor:uint = configXML.symbols.line.color || 0xFF0000;
    const resultLineSymbolAlpha:Number = configXML.symbols.line.alpha || 0.5;
    resultLineSymbol = new SimpleLineSymbol(resultLineSymbolStyle, resultLineSymbolColor, resultLineSymbolAlpha, resultLineSymbolWidth);
    
    //fillsymbol
    const resultFillSymbolStyle:String = configXML.symbols.polygon.style || 'solid';
    const resultFillSymbolColor:uint = configXML.symbols.polygon.color || 0xFF0000;
    const resultFillSymbolAlpha:Number = configXML.symbols.polygon.alpha || 0.5;
    const resultFillSymbolOutlineStyle:String = configXML.symbols.polygon.outlinestyle || 'solid';
    const resultFillSymbolOutlineColor:uint = configXML.symbols.polygon.outlinecolor || 0x000000;
    const resultFillSymbolOutlineAlpha:Number = configXML.symbols.polygon.outlinealpha || 1;
    const resultFillSymbolOutlineWidth:Number = configXML.symbols.polygon.outlinewidth || 1;
    resultFillSymbol = new SimpleFillSymbol(resultFillSymbolStyle, resultFillSymbolColor, resultFillSymbolAlpha, new SimpleLineSymbol(resultFillSymbolOutlineStyle, resultFillSymbolOutlineColor, resultFillSymbolOutlineAlpha, resultFillSymbolOutlineWidth));
    
    graphicsLayer = new GraphicsLayer();
    graphicsLayer.name = "acAdv Results";
    graphicsLayer.symbol = resultMarkerSymbol;
    map.addLayer(graphicsLayer);
   }


just delete your existing onQueryComplete function
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Mads,

    I also figured out that you need to catch the possible exception of getting a null value returned.

     function onQueryComplete(featureSet:FeatureSet, token:String = null):void 
     {
      for each (var graphic:Graphic in featureSet.features)
      {
       if(graphic.attributes[token]){
        var fieldValue:String = graphic.attributes[token].toString();
        results.addItem({label: fieldValue, data: graphic});
       }
      }   
      var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(featureSet.features);
      commExtent = graphicsExtent;    
     }
0 Kudos
LemvigKommune
Deactivated User
Thanks Robert

That just did it, i couldn't have solved that myself...

I hope my employer allows me to go to the Esri International User Conference in San Diego this summer, it would be a pleasure to meet you.

Thanks again.
Mads
0 Kudos
LeticiaZloccowick
New Contributor
Hi guys!

I'm doing almost the same thing here.
However, I need to show the results by layer, and also indicating the layer name (they will be displayed in different panels).
Is there a way to know the layer name inside the onQueryComplete function?

Thanks a lot!!!
Leticia
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Leticia,

   Sure you could pass that in to the query as a part of the token like we are doing with the layerOutField. So in stead of just passing in layerOutField  you could pass:

layerOutField +"|" + lyrList.layername
and then just split the string in the onQueryComplete function.
0 Kudos
LeticiaZloccowick
New Contributor
Hi Robert!

Thanks a lot!

Regards,
Leticia
0 Kudos