AnsweredAssumed Answered

How to count features in a polygon

Question asked by xupla on Mar 24, 2014
Latest reply on Apr 20, 2014 by xupla
I have published two layers. One containing points and the other containing polygons.

I would like to count the number of points belonging to a polygon when the user clicks it using ArcGIS Javascript API. I have already done the functionality of highlighting the polygon when the user clicks it. Now I want that an alert message will show up with the number of features in that polygon when the polygon is clicked. I already tried to do some code but its not giving the correct answer.

function selectByCouncil() {                 var localCouncils = new FeatureLayer("feature layer with polygons", {                     mode: FeatureLayer.MODE_SNAPSHOT,                     outFields: ["*"]                 });                  map.addLayer(localCouncils);                  var highlightSymbol = new SimpleFillSymbol(                   SimpleFillSymbol.STYLE_SOLID,                   new SimpleLineSymbol(                     SimpleLineSymbol.STYLE_SOLID,                     new Color([255, 0, 0]), 1),                   new Color([125, 125, 125, 0.35])                 );                  map.on("load", function () {           ;                 });                  localCouncils.on("click", function (evt) {           ;                     var highlightGraphic = new Graphic(evt.graphic.geometry, highlightSymbol);           ;                      var query = new Query();                     var queryTask = new QueryTask(feature layer with points);                     query.geometry = evt.graphic.geometry;                     query.returnGeometry = true;                      var countOfFeatures = 0;                      queryTask.execute(query, function (results) {                         dojo.forEach(results.features, function (feature) {                             countOfFeatures++;                                                     });                          alert("Number of points in polygon " + countOfFeatures);                     });                                      });             }

Any ideas on how this could be done are much appreciated. Thanks