AnsweredAssumed Answered

How to add QueryTask result to map as a new Layer

Question asked by markjones_tps on Jul 25, 2018
Latest reply on Jul 26, 2018 by markjones_tps

ok, i have given this the old college try but have reached a road block that I cannot seem to get past.

 

I have written the following code (added below as well as attached to this post) and am able to properly execute the QueryTask (as per the console output), but what I haven't been able to figure out is how to take the results, create a new layer from those results, and then add that layer to the map (i.e. a layer showing each point as a pin on the map). Any guidance, help, etc. is immensely appreciated.

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>API Demo for Crime App API</title>
  <link rel="stylesheet" href="https://js.arcgis.com/4.8/esri/css/main.css">
<script src="https://js.arcgis.com/4.8/"></script>  
<style>
html,
body,
#viewDiv {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
}
</style>
</head>
 <body>
  <div id="viewDiv"></div>
<script>
require([
  "esri/Map",
  "esri/views/MapView",
  "esri/layers/FeatureLayer",
  "esri/tasks/QueryTask",
  "esri/tasks/support/Query",
  "dojo/on",
  "dojo/dom",
  "dojo/domReady!"
], function(
  Map,
  MapView,
   FeatureLayer,
  QueryTask,
  Query,
   Popup,
   Search,
  on,
  dom
) { //TOP of REQUIRE
  // Code starts here
 var ftLayer = new FeatureLayer(
  {
    url: "https://services.arcgis.com/S9th0jAJ7bqgIRjw/ArcGIS/rest/services/YTD_Crime/FeatureServer/0",
    outFields: ["*"],
    definitionExpression:"mci_category = 'Shooting'"
  });
 
var qryLayerUrl = "https://services.arcgis.com/S9th0jAJ7bqgIRjw/ArcGIS/rest/services/YTD_Crime/FeatureServer/0";

 var queryTask = new QueryTask({
    url: qryLayerUrl
  });


var query = new Query();
  query.returnGeometry = true;
  query.outFields = ["*"];
  query.where = "mci_category = 'Shooting'";  
  queryTask.execute(query).then(function(results){
    console.log(results.features);
  });


queryTask.executeForCount(query).then(function(results){
    console.log(results);
  });

  // Create the map
  var map = new Map({
    basemap: "streets"
    
  });

  // Create the view
  var view = new MapView({
    container: "viewDiv",
    map: map,
    zoom: 10,
    center: [-79.3832, 43.6532]
  });

});

</script>
  </body>
</html>

 

Thanks,

Mark

Attachments

Outcomes