Query & QueryTask with a feature layer not shown until Query Task executed

2951
15
Jump to solution
03-06-2014 11:52 AM
MichelleRogers1
New Contributor III
I am trying to query a feature layer that is not shown on the map until the query is actually executed. I have a button that I am using to set off the query when it is clicked. Here is the code that I am trying to use:
on(dom.byId("mapshow"), "click", selectHistorical); function selectHistorical(){  var queryTask = new QueryTask(window.historicalUrl);  var query = new Query();  query.returnGeometry=true;  query.outFields=window.historicalOutFields;  query.text = dom.byId("mydropdown").value;  query.outSpatialReference = {"wkid":2236};  dojo.connect(queryTask, "onComplete", function(featureSet){   map.graphics.clear();   dojo.forEach(featureSet.features, function(feature){    var graphic = feature;    map.graphics.add(graphic);   });  });  queryTask.execute(query); }


The feature layer is already included in the webpage in on demand mode. What I am trying to do is query the layer so that the only items that show up from that layer are the items from one specific unit id. This is to show the history of one specific vehicle over time.  Eventually we would also want to query the time so that the user can specify a certain time and date to get the historical information for one specific vehicle.  I am posting a photo of the viewer.

[ATTACH=CONFIG]31992[/ATTACH]

The dropdown menu in the right panel has the unit id's for all police vehicles. What I want is for the historical data (in the historical feature layer) to be shown for whatever specific unit id is selected when I click on the show on map button.

I am fairly new to coding with esri, so any help is appreciated.
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
I was having trouble getting this to work with the wkid 2236, but was able to get it to work using a the geographic coordinate system 4326.  Here was the function I used:

function selectHistorical() {         var queryTask = new QueryTask(window.historicalUrl);         var query = new Query();         query.returnGeometry = true;         query.outFields = window.historicalOutFields;         query.where = "DeviceId = '" + dom.byId("mydropdown").value + "'";                     query.outSpatialReference = {             "wkid": 4326         };                              dojo.connect(queryTask, "onComplete", function (featureSet) {                             map.graphics.clear();             dojo.forEach(featureSet.features, function (feature) {                                                       var point = new Point(feature.geometry.x, feature.geometry.y, new SpatialReference({wkid:4326}));                 var simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10,                   new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,                   new Color([0,255,0,0.25]), 1),                   new Color([255,0,0])                 )                                                      var graphic = new Graphic(point, simpleMarkerSymbol);                                     map.graphics.add(graphic);             });         });                 queryTask.execute(query);     }


You will need to add the following modules:

SpatialReference, Point, SimpleMarkerSymbol, SimpleLineSymbol, and Graphic

View solution in original post

0 Kudos
15 Replies
AndyBurns
Occasional Contributor
Hi

Looks like your query is correct and i presume you can see the query execute if you use firebug or similar?

try the below, you need to take the results and do something with them. I pass it to the showResults function, build a table and zoom to the location on the map.

//execute query
          queryTask.execute(query0, showResults);
        };

        function showResults(results) {
          var s = "<table>";
          var aliases = results.fieldAliases; 
          var len = results.features.length;
          var fieldTypes = {};
          //var dateI = results.fields.length;

           // define a variable that will hold the field type for each field name.          
            array.forEach(results.fields, function (aField) {
            fieldTypes[aField.name] = aField.type;
          });

            if (len === 0) { // if no results
             s = "No results for IPMS reference: " + IPMSREF
            } else { // if 1 or more results, display them
             for (var i = 0; i < len; i++) {
              var featureAttributes = results.features.attributes; 
                for (var att in featureAttributes) {
                 //var aliasesFix = aliases[att].replace("*", "")
                  if (featureAttributes[att] == null) {
                   featureAttributes[att] = ""
              }
              else if (fieldTypes[att] == "esriFieldTypeDate") {
               featureAttributes[att] = formatDate(new Date(featureAttributes[att]));
              }
              else if (fieldTypes[att] == "esriFieldTypeDouble") {
                featureAttributes[att] = Math.round(featureAttributes[att]*10000)/10000
              }
              else if (att == "EDITEDBY" && "CREATEDBY") {
                featureAttributes[att] = featureAttributes[att].replace("IPS$",""); 
              }

           s = s + "<tr><td><span style='white-space: nowrap'>" + "<b>" + (aliases.hasOwnProperty(att) && aliases[att]) + "</b>" + ": " + "</td><td style='width:100%;'>" + featureAttributes[att] + "</td></tr>";
            }
            s = s + "<tr class='blank_row'><td></td>"
            }  
            s = s + "</table><br />";                 
          };
           dom.byId("info").innerHTML = s;
           dojo.query("#info table tr:nth-child(odd)").addClass("odd");
           dojo.query("#info table tr:nth-child(even)").addClass("even");

           //get x and y from centre of results geometry = true query
           var polyGonGeom = results.features[0].geometry;
           var polyGonExt = polyGonGeom.getExtent();
           map.setExtent(polyGonExt);

           var Polygon = new esri.geometry.Polygon(polyGonGeom, new esri.SpatialReference({
               wkid: 27700
            }));   

           console.log(Polygon)
           
            if (!graphic) {
                  addGraphic(Polygon);
                } else {
                  graphic.setGeometry(Polygon);
              }; 
        };
        //add graphic to the map
       function addGraphic(Polygon) {
        //parameters for the symbol 
        var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([49, 101, 156]), 2), new dojo.Color([148, 186, 231, 0.5]));
                // var symbol = new esri.symbol.PictureMarkerSymbol({
                //     "angle": 0,
                //     "xoffset": 0,
                //     "yoffset": 0,
                //     "type": "esriPMS",
                //     "url": "http://static.arcgis.com/images/Symbols/Basic/BlueShinyPin.png",
                //     "contentType": "image/png",
                //     "width": 24,
                //     "height": 24
                // });
                var graphic = new esri.Graphic(Polygon, symbol);
                map.graphics.add(graphic);
        };
0 Kudos
AndyBurns
Occasional Contributor
Tried:

query.where = "ID =" dom.byId("mydropdown").value;
instead of query.text??

replace ID with your rest url attribute column name.

Do you get a result with firebug to the call?
0 Kudos
MichelleRogers1
New Contributor III
Tried:

query.where = "ID =" dom.byId("mydropdown").value;
instead of query.text??

replace ID with your rest url attribute column name.

Do you get a result with firebug to the call?


I tried your suggestion, but it didn't like where the quotes were.  So, I moved the quotes to only include
"VehicleId" = dom.byId("mydropdown").value;


Now in internet explorer debugging tools, I am getting the error: SCRIPT5008: Invalid left-hand side in assignment

Also, when I first open the debugger, it is giving this error:
LOG: dojo.io.script errorError: Error performing query operation


Any idea why these errors are occuring?  It works for the data, just not the geometry.  I am able to pull the data into a grid, but the geometry doesn't show up on the map.  The code for showing up in the grid is as follows:
on(dom.byId("execute"), "click",execute);
      
function execute(){
 var queryTask = new QueryTask(window.historicalUrl);
 var query = new Query();
 query.text = dom.byId("mydropdown").value;
 query.returnGeometry = true;
 query.outFields = window.historicalOutFields;
 queryTask.execute(query, function (results) {
  var data = [];
  var data = array.map(results.features, function (feature) {
   return {
    "Vehicleid": feature.attributes[window.historicalOutFields[0]],
    "Velocity": feature.attributes[window.historicalOutFields[1]],
    "Direction": feature.attributes[window.historicalOutFields[2]],
    "TimeStamp": feature.attributes[window.historicalOutFields[3]]
   }
  });
  var memStore = new Memory ({data:data});
  window.grid3.set("store", memStore);
 });
};


Is there a way to just get the geometry into this code?  If so, it is preferable for one button click instead of two.  If it's a different query altogether, then I will have to go with the two buttons.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Michelle,

Could you create a jsfiddle with your code?  This will make it easier to troubleshoot.
0 Kudos
MichelleRogers1
New Contributor III
Hi Michelle,

Could you create a jsfiddle with your code?  This will make it easier to troubleshoot.


Jake,
Here is my code in a jsfiddle.  We are nearing the end of what needs to be done, and the historical data is the last of what we are trying to do with the viewer.
http://jsfiddle.net/mrogers83/Hz59f/1/
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I couldn't see if this resolved the problem since the services don't appear to be public facing, but try chaning the query to the following:

query.where = "DeviceId = '" + dom.byId("mydropdown").value + "'";
0 Kudos
MichelleRogers1
New Contributor III
I couldn't see if this resolved the problem since the services don't appear to be public facing, but try chaning the query to the following:

query.where = "DeviceId = '" + dom.byId("mydropdown").value + "'";


You are correct in saying that the services are not public facing.  This is for security reasons because we are tracking police vehicles.  I no longer get an error  message when I click the button, but I am not seeing anything show up on my screen either.  I have tried debugging through firefox and internet explorer since I am only working with Notepad ++ for development, but neither one gives me any indication of what is wrong that the geometry is not showing up on the map.
0 Kudos
AndyBurns
Occasional Contributor
do you have firebug installed? If so, check the response from the querytask and you should see the features and the geometry.

Let us know how you get on.
0 Kudos
MichelleRogers1
New Contributor III
do you have firebug installed? If so, check the response from the querytask and you should see the features and the geometry.

Let us know how you get on.


I just installed firebug.  Where would I see the response from the querytask?  I've never used firebug before.
0 Kudos