0

I have an application in Flex 4 with a map, a database of points and a search tool. When the user types something and does the search it returns name, details and coordinates of the objects in my database.

I have a function that, when i click one of the results of my search, it zooms the selected point of the map.

The question is, i want a function that zooms all the result points at once. For example if i search "tall trees" and it returns 10 points, i want that the map zooms to a position where i can see the 10 points at once.

Below is the code im using to zoom one point at a time, i thought flex would have some kind of function "zoom to group of points", but i cant find anything like this.

private function ResultDG_Click(event:ListEvent):void
        {

            if (event.rowIndex < 0) return;

            var obj:Object = ResultDG.selectedItem;

            if (lastIdentifyResultGraphic != null)
            {
                graphicsLayer.remove(lastIdentifyResultGraphic);
            }
            if (obj != null)
            {
                lastIdentifyResultGraphic = obj.graphic as Graphic;
                switch (lastIdentifyResultGraphic.geometry.type)
                {
                    case Geometry.MAPPOINT:
                        lastIdentifyResultGraphic.symbol = objPointSymbol
                        _map.extent = new Extent((lastIdentifyResultGraphic.geometry as MapPoint).x-0.05,(lastIdentifyResultGraphic.geometry as MapPoint).y-0.05,(lastIdentifyResultGraphic.geometry as MapPoint).x+0.05,(lastIdentifyResultGraphic.geometry as MapPoint).y+0.05,new SpatialReference(29101)).expand(0.001);

                        break;
                    case Geometry.POLYLINE:
                        lastIdentifyResultGraphic.symbol = objPolyLineSymbol;
                        _map.extent = lastIdentifyResultGraphic.geometry.extent.expand(0.001);

                        break;
                    case Geometry.POLYGON:
                        lastIdentifyResultGraphic.symbol = objPolygonSymbol;
                        _map.extent = lastIdentifyResultGraphic.geometry.extent.expand(0.001);

                        break;
                }
                graphicsLayer.add(lastIdentifyResultGraphic);

            }


        }
Bruno
  • 15
  • 5

1 Answers1

1

See the GraphicUtil class from com.esri.ags.Utils package. You can use the method "getGraphicsExtent" to generate an extent from an array of Graphics. You then use the extent to set the zoom factor of your map :

var graphics:ArrayCollection = graphicsLayer.graphicProvider as ArrayCollection;
var graphicsArr:Array = graphics.toArray();

// Create an extent from the currently selected graphics
var uExtent:Extent;

uExtent = GraphicUtil.getGraphicsExtent(graphicsArr);

// Zoom to extent created
if (uExtent) 
{
    map.extent = uExtent;
}    

In this case, it would zoom to the full content of your graphics layer. You can always create an array containing only the features you want to zoom to. If you find that the zoom is too close to your data, you can also use map.zoomOut() after setting the extent.

Note: Be careful if you'Ve got TextSymbols in your graphics, it will break the GraphicUtil. In this case you need to filter out the Graphics with TextSymbols

Derp : Did not see the thread was 5 months old... Hope my answer helps other people

Ggilmann
  • 79
  • 1
  • 12