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);
}
});
Solved! Go to Solution.
Interesting. So you need to manually define the query out spatial reference then
query.outSpatialReference = {wkid:102100};
Bengi,
You need to set the queries outSpatialReference to the maps:
query.outSpatialReference = map.spatialReference;
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!
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);
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}}
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?
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);
}
});
Bengi,
Try changing this line for testing:
query.outFields = ["*"];
You are right it is actually adding the points to graphiclayer but somewhere in Africa!. Is there any way to update the spatialRefrence?
and when I try to get map spatial Reference
console.log(map.spatialReference);
it outputs undefine