Not Able To Display Query Task Result on Graphic Layer

1698
12
Jump to solution
09-27-2017 10:07 AM
BehrouzHosseini
Occasional Contributor

Using ArcGIS API 3.21 and following code I am trying to populate the result of a Query Task on map by populating the result featureSet on GraphicLayer but I am not getting any thing in result. and No error on Console. Can you please let me know what I am doing wrong?

var map;
 require([
 "esri/map", 
 "esri/layers/FeatureLayer", 
 "esri/layers/GraphicsLayer",
 "esri/tasks/query",
 'esri/tasks/QueryTask',
 'dojo/_base/array',
 "esri/graphic", 
 "esri/symbols/SimpleMarkerSymbol",
 "esri/Color", 
 "dojo/dom", 
 "dojo/domReady!"
 ], function(
 Map, FeatureLayer, GraphicsLayer, Query, QueryTask, array, Graphic, SimpleMarkerSymbol, Color, dom
 ) {
map = new Map("mapDiv", { 
 basemap: "streets",
 center: [-88.155000, 41.784042],
 zoom: 14,
 slider: false
 });
 
 var graphicsLayer = new GraphicsLayer();
 
 var markerSymbol = new SimpleMarkerSymbol(
 SimpleMarkerSymbol.STYLE_SQUARE, 10, null, new Color([50,50,255])
 );
 
  var queryTask = new QueryTask("https://server.domain.com/arcgis/rest/services/ElectricData/FeatureServer/3");
   
  var query = new Query();
   query.where = "1=1";
   query.returnGeometry = true;
   query.outFields = [ "CreationUser"];
   queryTask.execute(query).then(onQuerySuccess, onError);

  function onQuerySuccess(featureSet) {
    array.forEach(featureSet.features, function(feature) {
    feature.setSymbol(markerSymbol);
    graphicsLayer.add(feature);
   });
   map.addLayer(graphicsLayer);
  }

  function onError(error) {
    console.error('An error ocurred in the query: ', error);
 }
 
 });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Interesting. So you need to manually define the query out spatial reference then

query.outSpatialReference = {wkid:102100};

View solution in original post

0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus

Bengi,

   You need to set the queries outSpatialReference to the maps:

query.outSpatialReference = map.spatialReference;
0 Kudos
BehrouzHosseini
Occasional Contributor

Thanks for comment Robert I updated the code as:

query.where = "1=1";
 query.returnGeometry = true;
 query.outSpatialReference = map.spatialReference;
 query.outFields = [
 "CreationUser"
 ];

but still noting on the map!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Bengi,

   Are getting to the onQuerySuccess function? Can you add a console statement to that function to test?

The way I normally have my querytask execute is this:

queryTask.execute(query, onQuerySuccess, onError);
0 Kudos
BehrouzHosseini
Occasional Contributor

I also tried to console.log() the results

 console.log(featureSet.features[i])

which I am getting this points

{type: "point", x: 1034892.977614373, y: 1871653.8816519529, spatialReference{wkid: 102671, latestWkid: 3435}}

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Bengi,

   What is returning from the query is still not the correct spatial reference then it is 102671 when it needs to be 102100. Can you show your latest updated code?

0 Kudos
BehrouzHosseini
Occasional Contributor

Sure here you are

var map;
 require([
 "esri/map", 
 "esri/layers/FeatureLayer", 
 "esri/layers/GraphicsLayer",
 "esri/tasks/query",
 'esri/tasks/QueryTask',
 'dojo/_base/array',
 "esri/graphic", 
 "esri/symbols/SimpleMarkerSymbol",
 "esri/symbols/SimpleLineSymbol", 
 "esri/symbols/SimpleFillSymbol", 
 "esri/renderers/SimpleRenderer",
 "esri/Color", 
 "dojo/dom", 
 "dojo/domReady!"
 ], function(
 Map, FeatureLayer, GraphicsLayer, Query, QueryTask, array, Graphic, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, SimpleRenderer, Color, dom
 ) {
map = new Map("mapDiv", { 
 basemap: "streets",
 center: [-88.155000, 41.784042],
 zoom: 14,
 slider: false
 });
 var graphicsLayer = new GraphicsLayer();
 var markerSymbol = new SimpleMarkerSymbol(
 SimpleMarkerSymbol.STYLE_SQUARE, 10, null, new Color([50,50,255])
 );
 
 
 var queryTask = new QueryTask("https://server.domain.com/arcgis/rest/services/ElectricData/FeatureServer/3");
var query = new Query();
 
 
 query.where = "1=1";
 query.returnGeometry = true;
 query.outSpatialReference = map.spatialReference;
 query.outFields = [
 "CreationUser"
 ];
 queryTask.execute(query).then(onQuerySuccess, onError);
function onQuerySuccess(featureSet) {
array.forEach(featureSet.features, function(feature) {
feature.setSymbol(markerSymbol);
 graphicsLayer.add(feature);
});
 console.log(featureSet.features)
 map.addLayer(graphicsLayer);
 
}
function onError(error) {
console.error('An error ocurred in the query: ', error);
}
 
 
 });
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Bengi,

Try changing this line for testing:

query.outFields = ["*"];
0 Kudos
BehrouzHosseini
Occasional Contributor

You are right it is actually adding the points to graphiclayer but somewhere in Africa!. Is there any way to update the spatialRefrence?

0 Kudos
BehrouzHosseini
Occasional Contributor

and when I try to get map spatial Reference

console.log(map.spatialReference);

it outputs undefine

0 Kudos