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;

        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);


        var features = [];

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

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

Any idea how I could do this please?


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


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.


Esri_MapClick_Handler = dojo.connect(map, 'onClick', addToMap)

function addToMap(evt) { var x = evt.mapPoint.getLatitude(); var y = evt.mapPoint.getLongitude(); var latlog = new esri.geometry.Point(x, y);


} function activateTool() { toolbar.activate(esri.toolbars.Draw.POLYGON); //geometry.rings map.hideZoomSlider(); }

function createToolbar(map) { toolbar = new esri.toolbars.Draw(map); dojo.connect(toolbar, "onDrawEnd", PolygonAddToMap);


function PolygonAddToMap(geometry) { //alert(geometry.rings);//working ECreateGeoPolygon = false; var symbol; toolbar.deactivate(); map.showZoomSlider(); switch (geometry.type) { case "polyline": symbol = new esri.symbol.SimpleLineSymbol(); break; default: symbol = new esri.symbol.SimpleFillSymbol(); break; } AddGeoPolygon = new esri.Graphic(geometry, symbol); map.graphics.add(AddGeoPolygon); CheckPointInPolygon(geometry); } function CheckPointInPolygon(geometry) { var polygon = new esri.geometry.Polygon(); polygon.addRing(MassPoints); var point = new esri.geometry.Point(parseFloat(21.00), parseFloat(75.00)); alert(polygon.contains(point));//true MassPoints.length = 0; MassPoints = []; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.