Query task and function not drawing my polygons?

169
4
Jump to solution
04-26-2019 11:22 AM
JeremySingh1
New Contributor II

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<title>Intro to FeatureLayer - 4.11</title>

<link
rel="stylesheet"
href="https://js.arcgis.com/4.11/esri/themes/light/main.css"
/>
<script src="https://js.arcgis.com/4.11/"></script>

<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>

<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/tasks/support/Query",
"esri/tasks/QueryTask",
"esri/Graphic"
], function(Map, MapView, FeatureLayer, Query,QueryTask,Graphic) {
var map = new Map({
basemap: "topo"
});

var view = new MapView({
container: "viewDiv",
map: map,
center: [-73.98,40.74],
zoom: 13
});

/********************
* Add feature layer
********************/
// var featureLayer = new FeatureLayer({
// url:
// "https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer"
// });

//map.add(featureLayer);

var query = new Query();
query.where = "LINE ='BMT'";
query.outFields = [ "*" ];
query.returnGeometry = true;
var queryTask = new QueryTask({
url:"https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer"
});

queryTask.execute(query)
.then(function(result){
//console.log(result.features.length)

//*** ADD ***//
result.features.forEach(function(item){
var g = new Graphic({
geometry: item.geometry,
attributes: item.attributes,
symbol: {
type: "simple-marker",
color: "black",
width: 1.2,
style: "short-dot"
}

});
view.graphics.add(g);
});

});
});


</script>
</head>

<body>
<div id="viewDiv"></div>
</body>
</html>

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Jeremy,

   You have several issue in your code on closer inspection.

1. The querytask url has to point to a specific layer in the service.

2. The where clause has to use fields and values that are actually in the service.

Your url has one layer so the url would be:

var queryTask = new QueryTask({
url: "https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer/2"
});‍‍‍‍‍‍

But that layer does Not have a Line field in it so your query is invalid.

This is what it should be:

      var query = new Query();
query.where = "SubwayCounts_2016_LINE ='BMT'";
query.outFields = ["*"];
query.returnGeometry = true;
query.outSpatialReference = view.spatialReference;
var queryTask = new QueryTask({
url: "https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer/2"
});

queryTask.execute(query)
.then(function(result) {
//console.log(result.features.length)

//*** ADD ***//
result.features.forEach(function(item) {
var g = new Graphic({
geometry: item.geometry,
attributes: item.attributes,
symbol: {
type: "simple-marker",
color: "black",
width: 1.2,
style: "circle"
}

});
view.graphics.add(g);
});

});
});

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Jeremy,

   You just need to set the outSpatialReference of the query class to be the same as the view.spatialReference.

0 Kudos
JeremySingh1
New Contributor II

Thanks Robert, I still seem to have the issue. Im a bit new to this. 

My changes are below:


var query = new Query();
query.where = "LINE ='BMT'";
query.outFields = [ "*" ];
query.outSpatialReference = view.spatialReference; *******Added this line.
query.returnGeometry = true;
var queryTask = new QueryTask({
url:"https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer"
});

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Jeremy,

   You have several issue in your code on closer inspection.

1. The querytask url has to point to a specific layer in the service.

2. The where clause has to use fields and values that are actually in the service.

Your url has one layer so the url would be:

var queryTask = new QueryTask({
url: "https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer/2"
});‍‍‍‍‍‍

But that layer does Not have a Line field in it so your query is invalid.

This is what it should be:

      var query = new Query();
query.where = "SubwayCounts_2016_LINE ='BMT'";
query.outFields = ["*"];
query.returnGeometry = true;
query.outSpatialReference = view.spatialReference;
var queryTask = new QueryTask({
url: "https://services1.arcgis.com/JPUKRee8mEBfJ0K4/arcgis/rest/services/Subway/FeatureServer/2"
});

queryTask.execute(query)
.then(function(result) {
//console.log(result.features.length)

//*** ADD ***//
result.features.forEach(function(item) {
var g = new Graphic({
geometry: item.geometry,
attributes: item.attributes,
symbol: {
type: "simple-marker",
color: "black",
width: 1.2,
style: "circle"
}

});
view.graphics.add(g);
});

});
});

View solution in original post

JeremySingh1
New Contributor II

Thank you! this works!

0 Kudos