0

I have carried out a QueryTask on a map service that returns a polygon when I input a postcode. Within the same service there is an additional layer that allows the user to query a local authority name on. I would like to carry out a querytask on this layer at the same time so that the map returns a polygon relating to the postcode input and also tells me what local authority it is contained within. I will ultimately show the local authority in a separate widget. Can anyone help please?

1

Correct me if I'm wrong, but it sounds like the layer with the local authority data is separate from the layer with the post code.

You'll need a second QueryTask for the layer with the local authority data. When the first QueryTask returns the polygon boundary of the post code, pass that polygon geometry into a Query.geometry parameter, and use the new query parameters in the second QueryTask. I've provided a little sample code below.

var postCodeQueryTask = new QueryTask(" /* insert your map service layer url for the post code */ ");
var localAuthQueryTask = new QueryTask(" /* insert map service url for the local authority data */ ");
var postParameters = new Query();

// do stuff to query the post code

// execute the queryTask for the post code
postCodeQueryTask.execute(postParameters, function (featureSet) {
  // do stuff with post code geometry

  // use the geometry to load the local authority data.
  var localAuthParam = new Query();
  localAuthParam.geometry = featureSet.features[0].geometry;
  // do other things to set up your new query

  localAuthQueryTask.execute(localAuthParam, function (localAuthFS) {
    // your featureSet with the local authority data is here.
  });

});
1

Take a look at this sample that shows how to work with queries of different layers or services. It also uses Dojo Promise function to handle the results from executing more than one query.

0

I've inclued the code I've done to return the postcode geometry but I'm still unsure about where I should add the 2nd queryTask to pass the geometry from the postcode to the local authority area. I'm gussing the 2nd queryTask should be included within the first so that it doesnt run before the 1st queryTask has finished?? Thanks var queryTask = new QueryTask("http://rest/services/live/SEARCH/MapServer/2");

     var query = new Query();
     query.returnGeometry = true;
     query.outFields = [
       "POSTCODE"
     ];

     on(dom.byId("execute"), "click", execute);

     function execute() {
         query.text = dom.byId("Postcode").value;
         queryTask.execute(query, showResults);
     }

     function showResults(results) {
         var resultItems = [];
         var resultCount = results.features.length;
         for (var i = 0; i < resultCount; i++) {
             var featureAttributes = results.features[i].attributes;

             var graphic = results.features[i];
             graphic.setSymbol(symbol);
             map.graphics.add(graphic);

             //var queryTask2 = new QueryTask("http://rest/services/live/SEARCH/MapServer/15");
             //query2 = new.esri.tasks.Query();
             //query2.returnGeometry - false;
             //query2.where = ""


             var strNum = 100;
             var newExtent = graphic.geometry.getExtent();
             newExtent.xmin = newExtent.xmin - strNum;
             newExtent.ymin = newExtent.ymin - strNum;
             newExtent.xmax = newExtent.xmax + strNum;
             newExtent.ymax = newExtent.ymax + strNum;
             map.setExtent(newExtent);

             for (var attr in featureAttributes) {
                 resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");
             }
             resultItems.push("<br>");
         }
         dom.byId("info").innerHTML = resultItems.join("");

     }
     dom.byId("results")

     function addToMap(result) {
         debugger;
     }
 });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.