Setting map extent to a group of graphics

3765
9
02-22-2011 06:51 PM
SamirGambhir
Occasional Contributor III
Hi,
I am running a query to select a number of polygons based on an intersecting polygon. The selected polygons are contiguous and are highlighted when the query is complete. I am trying to set the map extent to the extent of all these polygons combined together but have been unsuccessful. I have tried suggestions posted on these forums and using samples provided by ESRI but am still unable to get the map to zoom to this area. I have tried creating an array to push graphics inside it. I have also tried creating a polygon feature, a feature set and a graphicsLayer as a container to hold all the selected graphics, but it didn't work. I am using ArcGIS Server 9.3 with JavaScript API. Please help.
(Note: The code below has been extracted from a longer version. It might be missing some braces)

function findPolygonsInExtent(extent) {
      
  var graphics = map.graphics.graphics;     
  var attr =[];
  var items = [];
        var graphic;
  var len=0;
       
  for (var i=0, il=graphics.length; i<il; i++) {
          graphic = graphics;
          if (extent.intersects(graphic.geometry)) {
   len = len + 1;
   var results = [];
  
   graphic.setSymbol(highlightSymbol);
   var featureAttributes = graphic.attributes;
   for (att in featureAttributes) {
             var s = featureAttributes[att];
    results.push(s);
   }
  
   attr = results.slice(0,10);
   for(var j=0, jl=10; j<jl; j++)
   {
    items.push(attr);
   }
  
    } //if
   
          else if (graphic.symbol == highlightSymbol) {
            graphic.setSymbol(symbol);
          }
   
        } //for
  map.setExtent(//some polygon feature//.geometry.getExtent());
}
0 Kudos
9 Replies
HemingZhu
Occasional Contributor III
Hi,
I am running a query to select a number of polygons based on an intersecting polygon. The selected polygons are contiguous and are highlighted when the query is complete. I am trying to set the map extent to the extent of all these polygons combined together but have been unsuccessful. I have tried suggestions posted on these forums and using samples provided by ESRI but am still unable to get the map to zoom to this area. I have tried creating an array to push graphics inside it. I have also tried creating a polygon feature, a feature set and a graphicsLayer as a container to hold all the selected graphics, but it didn't work. I am using ArcGIS Server 9.3 with JavaScript API. Please help.
(Note: The code below has been extracted from a longer version. It might be missing some braces)

function findPolygonsInExtent(extent) {
      
  var graphics = map.graphics.graphics;     
  var attr =[];
  var items = [];
        var graphic;
  var len=0;
       
  for (var i=0, il=graphics.length; i<il; i++) {
          graphic = graphics;
          if (extent.intersects(graphic.geometry)) {
   len = len + 1;
   var results = [];
  
   graphic.setSymbol(highlightSymbol);
   var featureAttributes = graphic.attributes;
   for (att in featureAttributes) {
             var s = featureAttributes[att];
    results.push(s);
   }
  
   attr = results.slice(0,10);
   for(var j=0, jl=10; j<jl; j++)
   {
    items.push(attr);
   }
  
    } //if
   
          else if (graphic.symbol == highlightSymbol) {
            graphic.setSymbol(symbol);
          }
   
        } //for
  map.setExtent(//some polygon feature//.geometry.getExtent());
}


the utility function esri.graphicsExtent(graphics) returns the extent of graphics. It included in javascript api 1.6. So it should wok on ArcGIS server 9.3. If not, you just have to create a logic in you for loop for (var i=0, il=graphics.length; i<il; i++)  to calculate an extent that contains all your graphics. let me know if you need more help on how.
0 Kudos
SamirGambhir
Occasional Contributor III
Thanks Heming,
I looked up ARcGIS resource Center but could not find any example to use esri.graphicsExtent function. I'll really appreciate it if you can guide me a bit more.
Thanks
Samir
0 Kudos
HemingZhu
Occasional Contributor III
Thanks Heming,
I looked up ARcGIS resource Center but could not find any example to use esri.graphicsExtent function. I'll really appreciate it if you can guide me a bit more.
Thanks
Samir


Here is link: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/namespace_esri.htm#esri.graphicsExtent . could use this method like this. var extent =esri.graphicsExtent(map.graphics.graphics)... map.setExtent(extent).
0 Kudos
SamirGambhir
Occasional Contributor III
Thanks Heming,
I am not sure where to put his piece of code in my code. If you notice, I am selecting a subset of graphics from the overall set of graphics based on the intersecting criteria. I need to set the extent to the selected subset of graphics only.
Thanks again,
Samir
0 Kudos
HemingZhu
Occasional Contributor III
Hi, 
I am running a query to select a number of polygons based on an intersecting polygon. The selected polygons are contiguous and are highlighted when the query is complete. I am trying to set the map extent to the extent of all these polygons combined together but have been unsuccessful. I have tried suggestions posted on these forums and using samples provided by ESRI but am still unable to get the map to zoom to this area. I have tried creating an array to push graphics inside it. I have also tried creating a polygon feature, a feature set and a graphicsLayer as a container to hold all the selected graphics, but it didn't work. I am using ArcGIS Server 9.3 with JavaScript API. Please help. 
(Note: The code below has been extracted from a longer version. It might be missing some braces) 

function findPolygonsInExtent(extent) { 
  
var graphics = map.graphics.graphics;  
var attr =[]; 
var items = []; 
var graphic; 
var len=0; 
   var intersectedGraphics =[];
  
for (var i=0, il=graphics.length; i<il; i++) { 
graphic = graphics
if (extent.intersects(graphic.geometry)) { 
   intersectedGraphics.push(graphic);
len = len + 1; 
var results = []; 
  
graphic.setSymbol(highlightSymbol);  
var featureAttributes = graphic.attributes; 
for (att in featureAttributes) { 
var s = featureAttributes[att]; 
results.push(s); 

  
attr = results.slice(0,10); 
for(var j=0, jl=10; j<jl; j++) 

items.push(attr); 

  
} //if 
  
else if (graphic.symbol == highlightSymbol) { 
graphic.setSymbol(symbol); 

  
} //for 
   var extent =esri.graphicsExtent(intersectedGraphics);
map.setExtent(extent);

}



function findPolygonsInExtent(extent) {

var graphics = map.graphics.graphics;
var attr =[];
var items = [];
var graphic;
var len=0;
var intersectedGraphics =[];

for (var i=0, il=graphics.length; i<il; i++) {
graphic = graphics;
if (extent.intersects(graphic.geometry)) {
intersectedGraphics.push(graphic);
len = len + 1;
var results = [];

graphic.setSymbol(highlightSymbol);
var featureAttributes = graphic.attributes;
for (att in featureAttributes) {
var s = featureAttributes[att];
results.push(s);
}

attr = results.slice(0,10);
for(var j=0, jl=10; j<jl; j++)
{
items.push(attr);
}

} //if

else if (graphic.symbol == highlightSymbol) {
graphic.setSymbol(symbol);
}

} //for
var extent =esri.graphicsExtent(intersectedGraphics);
map.setExtent(extent);

}
0 Kudos
SamirGambhir
Occasional Contributor III
Hi Heming,
This works great! Thanks for all your help.
Samir
0 Kudos
JasonMiller1
New Contributor
I'm doing this same thing, using the same method.  It works for me some of the time.  I have a map that puts a route on there, similar to driving directions you'd get from Google maps and whatnot.  Sometimes, it zooms in perfectly.  Sometimes it zooms in too far, so it cuts the part of the route off.  Any idea what would cause this?
0 Kudos
HemingZhu
Occasional Contributor III
I'm doing this same thing, using the same method.  It works for me some of the time.  I have a map that puts a route on there, similar to driving directions you'd get from Google maps and whatnot.  Sometimes, it zooms in perfectly.  Sometimes it zooms in too far, so it cuts the part of the route off.  Any idea what would cause this?


Use the map.setExtent(yourExtent, true) instead of map.setExtent(yourextent). It will garantee that your extent will fully show.
0 Kudos
JasonMiller1
New Contributor
So simple, but that works like a champ.  Thanks for your help!
0 Kudos