rohitgarg83

Selecting Points under Polygon

Discussion created by rohitgarg83 on Aug 31, 2010
Latest reply on Sep 2, 2010 by rohitgarg83
Hello All,

I have created polygon, now i want to select points under polygon on click... please help me..

this is my code:-

[HTML]

  <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6" type="text/javascript"></script>

  <script type="text/javascript">
    dojo.require("esri.map");
    dojo.require("esri.tasks.geometry");
    dojo.require("esri.geometry");

    var map = null;
    var gsvc = null;
    var qtask = null;
    var polygonGraphic = null;
    var queryGraphic = null;

    function initialize() {
      map = new esri.Map("map");
      dojo.connect(map, "onLoad", function () {
        drawPolygon();
      });
     
   var layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://myserver:8399/arcgis/rest/services/Data_Demo/MapServer/");
   map.addLayer(layer);
  
      map.setExtent(new esri.geometry.Extent(72.81, 18.95, 72.85, 18.98, new esri.SpatialReference({wkid: 4326})));

      gsvc = new esri.tasks.GeometryService("http://myserver:8399/arcgis/rest/services/Geometry/GeometryServer");
     
   qtask = new esri.tasks.QueryTask("http://myserver:8399/arcgis/rest/services/Data_Demo/MapServer/2");
}
function drawPolygon() {
      var latOffset, lonOffset, center, lat, lon, points;
       center = map.extent.getCenter();

     var Lat = 76.85; var Lon = 26.298; var Lat1= 72.8296; var Lng1= 18.99522;
     var Lat2= 72.819175; var Lng2= 18.970838; var Lat3= 72.828; var Lng3= 18.969;
     var Lat4= 72.832; var Lng4= 18.951; var Lat5= 72.823697;var Lng5= 18.946847;
    var Lat6= 72.826775035; var Lng6= 18.93383939; var Lat7 = 72.8150861175;
   var Lng7 = 18.96397220334;var Lat8 = 72.81860278088;var Lng8 = 18.951649999;

    var polygon  = {"rings":[[[Lat1,Lng1],[Lat3,Lng3],[Lat6,Lng6], [Lat5,Lng5],[Lat8,Lng8],[Lat7,Lng7], [Lat2,Lng2], [Lat1,Lng1]]],
  "spatialReference":{" wkid":4326 }};

  var polygon = new esri.geometry.Polygon(polygon);

      var symbol = new esri.symbol.SimpleFillSymbol().setStyle(esri.symbol.SimpleFillSymbol.STYLE_SOLID);
      polygonGraphic = new esri.Graphic(polygon, symbol);
      map.graphics.add(polygonGraphic);
  
    }

    function doSimplify() {
gsvc.simplify([ polygonGraphic.geometry ], doQuery(polygonGraphic));
    }

    function doQuery(polygonGraphic) {
      var query = new esri.tasks.Query();
      //query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_INTERSECTS;
      query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_CONTAINS;
      query.geometry = polygonGraphic.geometry;
      query.returnGeometry = true;
      qtask.execute(query, queryCallback);
    }

    function queryCallback(featureSet) {
      map.graphics.remove(queryGraphic);
  
  var symbol = new esri.symbol.SimpleMarkerSymbol();
          symbol.style = esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE;
          symbol.setSize(8);
          symbol.setColor(new dojo.Color([255,255,0,0.5]));

      var features = featureSet.features;
      var point = new esri.geometry.Point();
      queryGraphic = new esri.Graphic(point,symbol)
      map.graphics.add(queryGraphic);

    }

    dojo.addOnLoad(initialize);
  </script>

</head>

<body class="tundra">
 
  <input type="button" value="Simplify the polygon and Do a Query with simplified polygon" onclick="doQuery(polygonGraphic);" />
    <div id="map" style="width:600px; height:400px; border:1px solid #000;"></div>
</body>

</HTML>

Outcomes