Select features by drawing a rectangle on the map

4479
3
10-21-2010 09:03 AM
bhbh
by
New Contributor
hi ,all

i want to select features on the map by drawing a rectangle and i use the following js code,but i can't get any feature selected.

dojo.require("esri.map");
   dojo.require("esri.toolbars.draw");
dojo.require("esri.toolbars.navigation");
dojo.require("esri.tasks.query");

var map,mapdraw,tiledlayer,dynamiclayer;

function init(){
  map = new esri.Map("map");
  mapdraw = new esri.toolbars.Draw(map);
 
  dojo.connect(mapdraw,"onDrawEnd",drawEnd);
 
  tiledlayer = new esri.layers.ArcGISTiledMapServiceLayer("http://mylady-pc/ArcGIS/rest/services/image/MapServer");
  dynamiclayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://mylady-pc/ArcGIS/rest/services/data/MapServer");
 
  map.addLayer(tiledlayer);
  map.addLayer(dynamiclayer);
}

function draw(){
  mapdraw.activate(esri.toolbars.Draw.EXTENT);
}

function drawEnd(geometry){
  map.graphics.clear();
  var  symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NONE,
             new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT,
             new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.25]));
  var graphic = new esri.Graphic(geometry,symbol);
  map.graphics.add(graphic);
  queryByGeometry(geometry);
  mapdraw.deactivate();
}

function queryByGeometry(geometry){
  var query = new esri.tasks.Query();
  var querytask = new esri.tasks.QueryTask("http://mylady-pc/ArcGIS/rest/services/data/MapServer/0");
  dojo.connect(querytask,"onComplete",queryComplete);
 
  query.returnGeometry = true;
  query.outFileds = ["Name","Mono"];
  query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_CONTAINS;
  querytask.execute(query);
}

function queryComplete(featureSet){
  alert(featureSet.features.length);
}

dojo.addOnLoad(init);
0 Kudos
3 Replies
bhbh
by
New Contributor
addtionally, i use arcgis server 9.3 and the version of js api is 1.6

thx for any suggestion!
0 Kudos
bhbh
by
New Contributor
problem solved =_=a

but what should i do if i want to query in multi layer?
it seems some performance fault if i use a loop query in all the layers contained in mapservice?

thx for help!
0 Kudos
AxelSchaefer
New Contributor II
In general: Querying more than 1 layer is less performant than querying only 1 layer. You have to test how the performance is. If you have to query through all the layers, you have to. Only your end-user can say if he *really* needs it. Otherwise you should strip the functionality down to the things that are really needed. This makes the application behave smoother and also easier to understand. 😉
0 Kudos