var polygonFeature = new FeatureLayer("feature layer with polygons", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); map.on("update-end", function () { array.forEach(polygonFeature.graphics, function (feature) { var name = feature.attributes.NAME; var query = new Query(); var queryTask = new QueryTask("feature layer with points"); query.geometry = feature.geometry query.returnGeometry = true; queryTask.executeForCount(query, function (count) { console.log("Locality " + name + " has " + count + " points"); }); }); });
Solved! Go to Solution.
<!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="http://js.arcgis.com/3.9/js/esri/css/esri.css"> <style> html, body, #map { height: 100%; width: 100%; margin: 0; padding: 0; } body { background-color: #FFF; overflow: hidden; font-family: "Trebuchet MS"; } </style> <script src="http://js.arcgis.com/3.9/"></script> <script> var map; require(["esri/map", "esri/layers/FeatureLayer", "esri/tasks/query", "esri/tasks/QueryTask", "dojo/on", "dojo/_base/array", "dojo/domReady!"], function (Map, FeatureLayer, Query, QueryTask, on, array) { map = new Map("map", { basemap: "topo", center: [-122.45, 37.75], // longitude, latitude zoom: 7 }); var polygonLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); var pointsLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); map.addLayers([polygonLayer, pointsLayer]); on.once(map, "update-end", function () { var query = new Query(); query.where = "STATE_NAME = 'California'"; var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"); queryTask.executeForCount(query, function (count) { console.log("California has " + count + " cities"); }); //array.forEach(polygonLayer.graphics, function (feature) { // var name = feature.attributes.STATE_NAME; // var query = new Query(); // var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"); // query.geometry = feature.geometry // query.returnGeometry = true; // queryTask.executeForCount(query, function (count) { // console.log("Locality " + name + " has " + count + " points"); // }); //}); }); }); </script> </head> <body> <div id="map"></div> </body> </html>
require(["dojo/on"], function(on){ on.once(map, "update-end", function(){ // will only fire once... }); });
function countReport() { require(["dojo/on", "dojo/_base/array", "esri/layers/FeatureLayer", "esri/tasks/query", "esri/tasks/QueryTask", "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol", "esri/graphic", "dojo/_base/Color"], function (on, array, FeatureLayer, Query, QueryTask, SimpleFillSymbol, SimpleLineSymbol, Graphic, Color) { var polygonFeature = new FeatureLayer("polygon feature layer", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); map.addLayer(polygonFeature); on.once(map, "update-end", function () { array.forEach(polygonFeature.graphics, function (feature) { var name = feature.attributes.NAME; var query = new Query(); var queryTask = new QueryTask("points feature layer"); query.geometry = feature.geometry query.returnGeometry = true; queryTask.executeForCount(query, function (count) { console.log("Locality " + name + " has " + count + " points"); }); }); }); }); }
<!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="http://js.arcgis.com/3.9/js/esri/css/esri.css"> <style> html, body, #map { height: 100%; width: 100%; margin: 0; padding: 0; } body { background-color: #FFF; overflow: hidden; font-family: "Trebuchet MS"; } </style> <script src="http://js.arcgis.com/3.9/"></script> <script> var map; require(["esri/map", "esri/layers/FeatureLayer", "esri/tasks/query", "esri/tasks/QueryTask", "dojo/on", "dojo/_base/array", "dojo/domReady!"], function (Map, FeatureLayer, Query, QueryTask, on, array) { map = new Map("map", { basemap: "topo", center: [-122.45, 37.75], // longitude, latitude zoom: 7 }); var polygonLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); var pointsLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0", { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); map.addLayers([polygonLayer, pointsLayer]); on.once(map, "update-end", function () { var query = new Query(); query.where = "STATE_NAME = 'California'"; var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"); queryTask.executeForCount(query, function (count) { console.log("California has " + count + " cities"); }); //array.forEach(polygonLayer.graphics, function (feature) { // var name = feature.attributes.STATE_NAME; // var query = new Query(); // var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"); // query.geometry = feature.geometry // query.returnGeometry = true; // queryTask.executeForCount(query, function (count) { // console.log("Locality " + name + " has " + count + " points"); // }); //}); }); }); </script> </head> <body> <div id="map"></div> </body> </html>