[GIS] Calculate the number of points in a freehand polygon ArcGis Javascript API

arcgis-javascript-apigraphicspolygon

I'm drawing a free hand polygon on my map. Now I want to calculate the number of points found in that polygon.

I have done this code, which my now is giving me the number of points found in the map extent rather than the polygon. Now I'm stuck how I'm going to do it since I am new to JavaScript.

function computeZonalStats(evtObj) {
        var geometry = evtObj.geometry;
        map.showZoomSlider();
        map.graphics.clear();

        var symbol = new SimpleFillSymbol("none", new SimpleLineSymbol("dashdot", new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]));
        var graphic = new Graphic(geometry, symbol);

        map.graphics.add(graphic);
        toolbar.deactivate();

        var features = [];
        features.push(graphic);

        var featureSet = new FeatureSet();
        featureSet.features = features;

        var count = 0;
        dojo.forEach(featureLayer.graphics, function (feature) {
                count++;
        });
        alert(count);
    }        

Any idea how I could do this please?

Best Answer

the polygon geometry object includes a method called "contains", so you could loop through your points on the clientside to determine whether each one falls within the freehand sketch

https://developers.arcgis.com/javascript/jsapi/polygon-amd.html#contains

another option would be to pass the sketch geometry back to the service providing the points in a spatial query that returns ObjectIds only to get a count.