Select to view content in your preferred language

Help! - Cannot add dynamically created polygon (sfv 1.3, agis 9.3.1)

780
4
10-05-2010 05:48 AM
grahamcooke
Regular Contributor
Hi,

I have created a polygon dynamically in my code behind. I am trying to add it into a graphics layer  and show it on the map.

Everything seems to be working ok as i step through, but the polygon is not displaying.

the FPClass.PlotTrace method returns a polygon and the code is as follows:
public function PlotTrace():Polygon

            {
                  firingPosX = parseInt(fpFirePoint.toString().substr(0,6));
           firingPosY = parseInt(fpFirePoint.toString().substr(6,6));
               
                  //if a min range has been set, set up the slip bearings to change the plotting of the trace

                  if (minRange > 0)
                  {
//plot a slipped trace
                  }

                  if (hefLength > 0 && stdTraceLength == 0)

                  {
                       //plot a hef trace
                  }

                  else
                  {
               myTrace = new Polygon();
               myTrace.addRing(createFFAFiringPoint());

                  }
                  return myTrace;
            }


createFFAFiringpoint returns an array of points used to create the polygon ring: myTrace shows in the debugger as follows:
myTrace            com.esri.ags.geometry.Polygon (@106b9b79)      
            [inherited]          
            defaultSymbol    com.esri.ags.symbol.SimpleFillSymbol (@10599d29)       
            extent   <exception thrown by getter>      
            rings     Array (@abb0ee9)          
                        [0]        Array (@1070d901)        
                                    [0]        flash.geom.Point (@10580629)    
                                                length   717464.8361703869       
                                                x          382415 [0x5d5cf]           
                                                y          607054 [0x9434e]          
                                    [1]        flash.geom.Point (@1055ce71)    
                                                length   717308.4317941585       
                                                x          382020.11968464445     
                                                y          607117.7929192365       
                                    [2]        flash.geom.Point (@1065e629)    
                                                length   717425.8993229957       
                                                x          381281.23903104366     
                                                y          607720.7728738267       
                                    [3]        flash.geom.Point (@10782fb1)     
                                                length   717649.1139426031       
                                                x          381252.6866894122       
                                                y          608002.1707483521       
                                    [4]        flash.geom.Point (@10782fd9)     
                                                length   718632.3713444908       
                                                x          383550.0679880897       
                                                y          607718.5454554912       
                                    [5]        flash.geom.Point (@10782241)    
                                                length   717728.7598126691       
                                                x          382810.00481118547     
                                                y          607117.0174510777       
                                    [6]        flash.geom.Point (@10580629)    
                                                length   717464.8361703869       
                                                x          382415 [0x5d5cf]           
                                                y          607054 [0x9434e]          
                                    length   7          
            type      "esriGeometryPolygon"  


but the polygon doesn't appear on the map. i try to get the polygon from an instantiation of the FPClass, use it to create a graphic and then add the graphic to the graphics layer as follows:
            private function PlotTrace(piTraceType:String):void
            {
                  switch (piTraceType)
                  {
                       case ("FirePoint"):
                        {        if (vwHefStd.selectedChild.id=="StdTemplate")
                              {
                                    var FPClass:FiringPointTrace = new FiringPointTrace(parseInt(txtFPCoord.text), parseInt(txtFPLeft.text), parseInt(txtFPRight.text), parseInt(txtStdLength.text),parseInt(txtStdWidth.text), parseInt(txtStdBSD.text), chkHT.selected,0,0,0,0,parseInt(txtStdAimErr.text), parseInt(txtFPMinRange.text))

                                    var polyGeometry:Geometry = new Geometry() as Polygon;
                                    polyGeometry = FPClass.PlotTrace();
                                    graphic = new Graphic(polyGeometry,sfs_dashedoutline);
                                    gfxTemplates.add(graphic);
                              }
                              //else do hef template
                              {
                              }
                        }
                        //case 2
                        //case 3
                  }
            }


I have created gfxtemplates in the init method for the widget and added the layer to the map in the same routine.

I must be missing something but im not sure what!

hope someone can help??
Tags (2)
0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus
Graham,

    I seem to remember running into this issue and I had to add a spatial reference to each point or it might have been just the polygon...
0 Kudos
grahamcooke
Regular Contributor
Hi Robert,
Thanks for your reply. I have made sure that I add a spatial reference to the points. I also realised they were points and not MapPoints so I have changed them to mappoints.

here is my code (code snips are edited down to keep this post simple)
private function createFFAFiringPoint():Array
                        {
var point:MapPoint;
                                    var i:int;
                                    var common:CommonRoutines = new CommonRoutines();
                                    var spatRef:SpatialReference = new SpatialReference(27700);

                                    try
                                    {
                                                //plot a standard trace
                                                //first of all adjust the left and right of arc figures to cater for aim error and run through the routine that ensures bearings 
 //are kept within the range of 6400mils (360 degs).
                                              arcLeft = common.MODO((arcLeft - aimError), 6400);
                                                arcRight = common.MODO((arcRight + aimError), 6400);
//calculate points on polygon to generate trace shape (there are always 6 points excluding firing position on standard ffa trace)
//we can't do a simple loop because although the method of calculating the point is always the same, the values we pass to the method
//vary depending on which point of the polygon we need to calculate:
                                               //plot origin and push to array
                                                point = new MapPoint(firingPosX, firingPosY, spatRef);
                                                arrPoints = new Array;
                                                arrPoints.push(point);
                                                //Point 1
                        point = common.CalculatePoint((stdTraceWidth * -1), common.Distance1(stdTraceWidth), arcLeft, firingPosX, firingPosY);

[etc.....]                        
            return arrPoints;



calculate point code:


public function CalculatePoint(piWidth:Number, piDistance:Number, piArc:Number, firingPointX:Number, firingPointY:Number):MapPoint

                        {

                                    var direction:Number;

                                   var targetBearing:Number;
                                    var distance:Number;
                                    var xValue:Number;
                                    var yValue:Number;
                                    var spatRef:SpatialReference = new SpatialReference(27700);
                                    try
                                    {
                                                //Flex uses radians to calculate angles, user entered details in mils.
                                                direction = milsToRads(piArc);
                                                //make adjustments for targetBearing in different quadrants of an x/y grid.

                                                targetBearing = adjustAngleForQuadrant(piWidth, piDistance) + direction;

                                                //basic trig: the hypotonuse length = sqrt of the sum of the squares of the other 2 sides

                                                distance = Math.sqrt((piWidth * piWidth) + (piDistance * piDistance))

                                                

                                                //create the co-ordinates for the newly calculated point

                                                xValue = Math.sin(targetBearing) * distance + firingPointX;

                                                yValue = Math.cos(targetBearing) * distance + firingPointY;

                                                //nb some extra code may need to be added later if we start to deal with non Uk maps to determine which 
                                                //spatial reference to use
                                                myPoint = new MapPoint(xValue,yValue, spatRef);           
[etc]                                                
return myPoint;



I also ensured that the polygon was spatially referenced as well:


var spatRef:SpatialReference = new SpatialReference(27700);

var newTrace:Polygon = new Polygon();

newTrace = FPClass.PlotTrace()

newTrace.spatialReference = spatRef;

graphic = new Graphic(newTrace, s_lineSymbol);

gfxTemplates.add(graphic);         


however I am still having no luck....Could the problem be the symbol declaration?
<esri:SimpleLineSymbol id="s_lineSymbol" color="0xFF000" width="3" alpha="0.5"/>


I have also upgraded to the 3.5 sdk from adobe (was on 3.2 before). If you or anyone else has any other ideas I'd love to hear them!
0 Kudos
grahamcooke
Regular Contributor
Robert,

Your solution was the correct one. You do need to spatially reference the mappoints and the polygon.

If i had used a simple fill symbol and not a simple line symbol it would have appeared having implemented your solution. When I was tweaking around to find what was wrong I changed my simplefillsymbol to a simplelinesymbol for some reason and didnt change it back.

all sorted now. 🙂
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Graham,

    Good to here, I thought I remembered that being the issue.
0 Kudos