Zoom to Graphics Layer?

5039
6
08-07-2013 01:41 PM
ZachLiu1
Occasional Contributor II
So I have a group of graphics displayed on map, does anyone know how to center and zoom to those graphics?

I guess I need to play with the Extent objects but do not have a clear mind to do this.

Thanks:D
0 Kudos
6 Replies
JasonZou
Occasional Contributor III
var extGraphics = esri.graphicsExtent(inGraphics);
map.setExtent(extGraphics)

where inGraphics is the array of the graphics you like to zoom to. Hope this helps.
ZachLiu1
Occasional Contributor II
This is much easier than I expected. I remember I did something similar with Google Map API and I have to update the extent of each graphic to a overall extent object. Thanks very much!:D
0 Kudos
JasonZou
Occasional Contributor III
Glad to be able to help:)
0 Kudos
ZachLiu1
Occasional Contributor II
I know this code works generally, but when I tied to use it after executing a query task, I can not set map extent to the returned graphics layer. Don't know what goes wrong.

function showResults(featureSet){
                //remove all graphics on the maps graphics layer
                map.graphics.clear();

                //Performance enhancer - assign featureSet array to a single variable.
                var resultFeatures = featureSet.features;

                $.each(resultFeatures, function(index, featureItem){
                    featureItem.setSymbol(symbol);
                    featureItem.setInfoTemplate(infoTemplate);
                    map.graphics.add(featureItem);
                });

                var extGraphics = GraphicsUtils.graphicsExtent(map.graphics.graphics);
                map.setExtent(extGraphics);
            }
0 Kudos
JasonZou
Occasional Contributor III
var extGraphics = GraphicsUtils.graphicsExtent(map.graphics.graphics);
Check extGraphics value after executing the above line. If the value is not what you expected, check map.graphics.graphics to see how many graphics there. To be safe, I would use featureSet to get the graphics array.
ZachLiu1
Occasional Contributor II
My bad:(. I typed GraphicUtils instead of GraphicsUtils when I loaded the module.

The featureSet seems not a graphics list though, I used featureSet.features now.

Thanks!:D
0 Kudos