Creating a Polygon

This example demonstrates how to create a polygon using an array of vertices and set basic properties. This example uses the ILinearRing, IGeometry, ITerrainPolygon61, ICreator61 (CreatePolygon, GeometryCreator), IGeometryCreator (CreateLinearRingGeometry, CreatePolygonGeometry) INavigate61 (FlyTo), and IPosition61 (Copy, Pitch) properties and methods.

 

function CreatePolygon()

        {

            try

            {

                //

                // B.  Create linear ring

                //

                {

                    //B1. Create vertices double array, each point in format x,y,z

                    var cVerticesArray = [

                        -122.415025,  37.76059,  10, 

                        -122.415868,  37.760546, 11, 

                        -122.415922,  37.761244, 12, 

                        -122.415592,  37.761254, 13, 

                        -122.415557,  37.760973, 14, 

                        -122.415081,  37.76099,  15 

                    ];

 

 

                    // B2. Create linear ring using vertices

                    {

                        var cRing = sgworld.Creator.GeometryCreator.CreateLinearRingGeometry(cVerticesArray);

                    }

                }

 

                //

                // C. Create polygon geometry using linear ring

                //

                {

                    var cPolygonGeometry = sgworld.Creator.GeometryCreator.CreatePolygonGeometry(cRing, null);

                }

 

                //

                // D. Create polygon using polygon geometry

                //

                {

                    // D1. Set polygon input params

                    var nLineColor = 0xFF00FF00; // Abgr value -> solid green

                    var nFillColor = 0x7FFF0000; // Abgr value -> 50% transparent blue

                    var eAltitudeTypeCode = 0; //AltitudeTypeCode.ATC_TERRAIN_RELATIVE;

 

                    // D2. Create polygon

                    cPolygon = sgworld.Creator.CreatePolygon(cPolygonGeometry, nLineColor, nFillColor, eAltitudeTypeCode, 0, "Polygon");

                }

 

                //

                // E. FlyTo polygon

                //

                {

                    var cFlyToPos = cPolygon.Position.Copy();

                    cFlyToPos.Pitch = -89.0; // Set camera to look downward on polygon

                    sgworld.Navigate.FlyTo(cFlyToPos);

                }

            }

            catch (e)

            {

                alert("unexpected error: " + e.description);

            }

        }