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 () { map.graphics.enableMouseEvents(); }); localCouncils.on("click", function (evt) { map.graphics.clear(); var highlightGraphic = new Graphic(evt.graphic.geometry, highlightSymbol); map.graphics.add(highlightGraphic); 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); }); }); }
Solved! Go to Solution.
function selectByPolygon() { //change the feature layer according to the requirments. This feature layer should contain polygons var polygonLayer= new FeatureLayer("Feature layer with polygons", { "opacity": 0.5 }, { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); map.addLayer(polygonLayer); 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]) ); localCouncils.on("click", function (evt) { map.graphics.clear(); var highlightGraphic = new Graphic(evt.graphic.geometry, highlightSymbol); map.graphics.add(highlightGraphic); var query = new Query(); var queryTask = new QueryTask("feature layer with points"); query.geometry = evt.graphic.geometry; query.returnGeometry = true; query.outSpatialReference = map.spatialReference; query.outFields = ["*"]; queryTask.execute(query, function (results) { geometryService.labelPoints(evt.graphic.geometry, function (labelPoints) { var font = new Font("20px", Font.STYLE_NORMAL, Font.VARIANT_NORMAL, Font.WEIGHT_BOLDER); array.forEach(labelPoints, function (labelPoint) { var textSymbol = new TextSymbol( "X: " + number.format(labelPoint.x) + ", Y: " + number.format(labelPoint.y), font, new Color([0, 0, 0]) ); var labelPointGraphic = new Graphic(labelPoint, textSymbol); map.graphics.add(labelPointGraphic); }); }); }); }); }
You can use the following function to report how many points are within each polygon once the map is loaded:var polygonFeature= new FeatureLayer(my feature layer with polygons, { mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"] }); map.addLayer(polygonFeature); map.on("update-end", function(){ array.forEach(polygonFeature.graphics, function(feature){ var OID = feature.attributes.OBJECTID; var query = new Query(); var queryTask = new QueryTask("url to points"); query.geometry = feature.geometry query.returnGeometry = true; queryTask.executeForCount(query, function(count){ console.log("ObjectID " + OID + " has " + count + " points"); }) }) })
You will need to add the dojo/_base/array module to your code. For adding the text to your map, take a look at the TextSymbol class.