Selecting Points, displaying Graphics

392
2
09-23-2011 07:07 AM
MatthewBorr
New Contributor III
Wondering if someone could help. Hopefully an easy one.

When I use this format:
    
 dojo.connect(map, "onClick", executeQueryTask);

      function executeQueryTask(evt) {
        query.geometry = evt.mapPoint;
        queryTask.execute(query, showResults);
      }

      function showResults(featureSet) {

        dojo.forEach(featureSet.features,function(feature){
          var graphic = feature;
          graphic.setSymbol(symbol);
          map.graphics.add(graphic);
        
        });
      }


to select polygons from:

http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/1

Which is a polygon layer from the census map service, it works fine.

However, if I use layer 0, which is a point layer, it will not run the dojo.foreach() function, and will not place the selection graphic. Here is my new code:

<script type="text/javascript" language="Javascript">
      dojo.require("esri.map");
      dojo.require("esri.tasks.query");
   dojo.require("esri.toolbars.draw");
      dojo.require("dojox.color.Palette");

      var map, querytask, query;
      var selectionSymbol, infoTemplate;

      function init() {
        //create map
        map = new esri.Map("mapDiv");

        //create and add new layer
        var layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
        map.addLayer(layer);
  
  var imageParams = new esri.layers.ImageParameters();
        imageParams.layerIds = [0,1];
        imageParams.layerOption = esri.layers.ImageParameters.LAYER_OPTION_SHOW;
   var dynamicLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer", {imageParameters:imageParams});
        
  var layerDefinitions = [];
  layerDefinitions[0] = "CNTY_FIPS = '081'";

  dynamicLayer.setLayerDefinitions(layerDefinitions);
  map.addLayer(dynamicLayer);

        dojo.connect(map, "onClick", executeQueryTask);

        querytask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0");

        query = new esri.tasks.Query();
        query.returnGeometry = true;
  query.outFields = ["POP2000"];
  
  selectionSymbol = new esri.symbol.SimpleMarkerSymbol();
     symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
      symbol.setSize(10);
     symbol.setColor(new dojo.Color([255,255,0,0.5]));
  
  
      }

      function executeQueryTask(evt) {
       
     query.geometry = evt.mapPoint;

     querytask.execute(query, showResults);
      }

      function showResults(featureSet) {
        
        map.graphics.clear();
   dojo.forEach(featureSet.features,function(feature){
          var graphic = feature;
    
          graphic.setSymbol(selectionSymbol);

          map.graphics.add(graphic);
       
        });
      }

      dojo.addOnLoad(init);
    </script>


Any thoughts out there?
0 Kudos
2 Replies
derekswingley1
Frequent Contributor
The problem is probably that you're querying a point layer with a single point. To return results, the points would have to be exactly the same, which isn't likely to happen.

To get around this, create a small extent from your map click and use this extent as your query.geometry. Something like this: 

var tolerance = 5,
    pxWidth = map.extent.getWidth() / map.width,
    padding = tolerance * pxWidth;
    queryGeometry = new esri.geometry.Extent({
      'xmin': evt.mapPoint.x - padding,
      'ymin': evt.mapPoint.y - padding,
      'xmax': evt.mapPoint.x + padding,
      'ymax': evt.mapPoint.y + padding,
      'spatialReference': evt.mapPoint.spatialReference
    });
query.geometry = queryGeometry;
0 Kudos
MatthewBorr
New Contributor III
Thanks a million!!
0 Kudos