How can I use the QueryTask for two different polygons

1026
1
Jump to solution
08-24-2016 01:38 PM
jerrysalinas
New Contributor

Hi - I want to create a query, if a user click a county anywhere in the states of California or Texas, I would like to highlight the county and state and display a message “you are in the state of CA or TX”, but if they click in any other state, display a message, “you are not in the designated state”, along the county and State name? I can’t get it to query two different states (polygons) and display the message. I am not sure if I am on the right track. I've reviewed most of the samples, but still having some issues. Thanks in advance. Jerry

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
<title>Simple Map</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.17/esri/css/esri.css">
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script src="https://js.arcgis.com/3.17/"></script>
<script>
var map;

require(["esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/ImageParameters", "esri/tasks/QueryTask", "esri/tasks/query", "esri/symbols/SimpleMarkerSymbol", "esri/InfoTemplate", "dojo/_base/Color", "dojo/domReady!"], function (Map, ArcGISDynamicMapServiceLayer, ImageParameters, QueryTask, Query, SimpleMarkerSymbol, InfoTemplate, Color) {
map = new Map("map", {
basemap: "topo", //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
center: [-122.45, 37.75], // longitude, latitude
zoom: 5
});

//map.on("click", excuteQueryTask);

//var imageParameters = new ImageParameters();
//imageParameters.format = "jpeg"; //set the image type to PNG24, note default is PNG8.

//Takes a URL to a non cached map service.
var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer", {
"opacity": 0.5,
//"imageParameters" : imageParameters,
//"infoTemplate" : infoTemplate
});

map.addLayer(dynamicMapServiceLayer);

var queryTask = new QueryTask("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3"); //States
var queryTask1 = new QueryTask("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2"); //Counties

var query = new Query();
query.returnGeometry = true;
query.outFields = ["*"];

var query1 = new Query();
query.returnGeometry = true;
query.outFields = ["NAME", "STATE_NAME", "POP2000"]

var infoTemplate = new InfoTemplate("${*}");
var infoTemplate1 = new InfoTemplate("${Name}", "State Name : ${STATE_NAME}");

var symbol = new SimpleMarkerSymbol();
symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
symbol.setSize(10);
symbol.setColor(new Color([255, 255, 0, 0.5]));

map.on("click", excuteQueryTask);

function excuteQueryTask(evt) {

mappoint = evt.mapPoint;
query.geometry = mappoint;
queryTask.execute(query, showResults);
queryTask1.execute(query1, showResults1);

}

function showResults(featureSet) {
//remove all graphics on the maps graphics layer
map.graphics.clear();
map.infoWindow.clearFeatures();

//Performance enhancer - assign featureSet array to a single variable.
var resultFeatures = featureSet.features;

//Loop through each feature returned
for (var i = 0, il = resultFeatures.length; i < il; i++) {
//Get the current feature from the featureSet.
//Feature is a graphic
//var graphic = resultFeatures;
//graphic.setSymbol(symbol);

//Set the infoTemplate.
//graphic.setInfoTemplate(infoTemplate);

//Add graphic to the map graphics layer.
//map.graphics.add(graphic);
resultFeatures.setInfoTemplate(infoTemplate);

}
map.infoWindow.setFeatures(resultFeatures);
map.infoWindow.show(mappoint);
}
function showResults1(featureSet1) {

map.graphics.clear();
map.infoWindow.clearFeatures()

var resultFeatures1 = featureSet1.features;
for (var i = 0, il = resultFeatures1.length; i < il; i++) {

resultFeatures1.setInfoTemplate(infoTemplate1);
}
map.infoWindow.setFeatures(resultFeatures1);
map.infoWindow.show(mappoint)
// alert(" you are in the state " );
}
});
</script>
</head>

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

0 Kudos
1 Solution

Accepted Solutions
JohnGrayson
Esri Regular Contributor

There are many different ways this could be done, but in this particular case you don't need QueryTask as the attributes in the county service include the state name. Here's one possible approach: State Name from Popup - JSFiddle You can assign the InfoTemplate to the service sublayer then listen to when the Popup has a selected feature.  Based on the attributes of the selected feature you can update the contents of the Popup.  If the necessary information only exists as attributes in two different services or sublayers, then one of several different approaches could be taken.  In the extreme case of having to use two QueryTasks, the approach would be to use dojo/promise/all to wait for both QueryTasks to complete before inspecting the results of both calls.

View solution in original post

1 Reply
JohnGrayson
Esri Regular Contributor

There are many different ways this could be done, but in this particular case you don't need QueryTask as the attributes in the county service include the state name. Here's one possible approach: State Name from Popup - JSFiddle You can assign the InfoTemplate to the service sublayer then listen to when the Popup has a selected feature.  Based on the attributes of the selected feature you can update the contents of the Popup.  If the necessary information only exists as attributes in two different services or sublayers, then one of several different approaches could be taken.  In the extreme case of having to use two QueryTasks, the approach would be to use dojo/promise/all to wait for both QueryTasks to complete before inspecting the results of both calls.