1

I have an application that runs a queryTask on a map service layer and returns various feature results in a div when they click on a table of contents. However, I'd like it to automatically update when the user zooms in or out of the current map extent - is this possible to do by amending the queryTask code? My code at the moment:

function addFields(layerURL, layerids, fields) {
         var mapExtent = map.extent;
         var currentScale = map.getScale();

         if (currentScale > 200000) {

             $('#show2').empty();
             $('#show2').html('No data at this scale. Zoom in to view data');
             $('#show3').empty();
             $('#show3').html('No data at this scale. Zoom in to view data');

         } else {

             $('#show3').empty();

             //query the fields in extent
             var query3 = new Query();
             query3.returnGeometry = true;
             query3.outFields = [fields];
             query3.geometry = mapExtent;
             var queryTask3 = new QueryTask(layerURL + '/' + layerids);

             //resultItems.push("Before process");
             queryTask3.execute(query3, function (results3) {
                 ////resultItems.push("Before");
                 if (results3.features.length < 1) {
                     $('#show2').empty();
                     $('#show3').empty();
                     $('#show3').html('No results are available in this area, zoom out to explore further...')
                     $('#show2').html('No results are available in this area, zoom out to explore further...')
                 } else {

                 for (var k = 0; k < results3.features.length; k++) {
                     //debugger;
                     for (var j = 0; j < fields.length; j++) {

                         var checkForLink = results3.features[k].attributes[fields[j]];

                         if (checkForLink.indexOf('http://') == 0) {
                             $('#show3').append($('<a href="'+checkForLink+'">Find out more...</a>'));

                         } else {
                             $('#show3')
                             .append(checkForLink);
                                }
                         $('#show3').append($('<br />'));
                     }

                     $('#show3').append($('<hr />'));

                 }
                }
             });
  • What you'll want to do is use the map's "extent-change" event to fire the query task. When the event fires, one of the properties returned is the new extent. You can use that in your query. – kenbuja Sep 8 '15 at 13:43

Your Answer

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

Browse other questions tagged or ask your own question.