Adding Polygon

2161
4
Jump to solution
10-11-2015 10:28 PM
JulieBiju
Occasional Contributor

Hi all, Thank U.

I am trying to add a polygon in a button click.The below code wrote in button click.But the polygon not showing. If I will pass some points direct like this then it is working perfect.Where is the problem??

polygon1.addRing([[58.4245719470205, 23.582511030118049], [58.423799470824214, 23.594545845196812],[58.44002147094627, 23.5743298721676], [58.443798021239232, 23.578735174416867], [58.4245719470205, 23.582511030118049]]);

while (CMt <= (list.d.length - 1))

{

                                

                                    if (Points1 != "") {

                                        Points1 = Points1 + ",";

                                    }

                                    Points1 = Points1 + "[" + list.d[CMt].EPolyLong + "," + list.d[CMt].EPolyLat + "]";

                                    gname = list.d[CMt].EPolyGname;

                                    GDesc = list.d[CMt].EPolyGDesc;

                                    CMt = CMt + 1;

                                };

                                if( Points1 != "")

                                {

                                    AddPolygonGraphics(Points1, gname, GDesc);

                                    Points1 = "";

                                }

            function AddPolygonGraphics(Points1, gname, GDesc) {

                Points1 = "[" + Points1 + "]"

                var polygon1 = new Polygon(new SpatialReference({ wkid: 4326 }));

                 //polygon1.addRing([[58.4245719470205, 23.582511030118049], [58.423799470824214, 23.594545845196812],[58.44002147094627, 23.5743298721676], [58.443798021239232, 23.578735174416867], [58.4245719470205, 23.582511030118049]]);

                polygon1.addRing(Points1);

//                alert(polygon1.rings[0].length);

                // map is in web mercator so transform the geometry 

                var poly_wm = webMercatorUtils.geographicToWebMercator(polygon1);

                var symbol = new SimpleFillSymbol();

                var strhotspot = (gname.trim());

                if (GDesc != "")

                {

                    strhotspot = strhotspot + "," + (GDesc.trim());

                }

                var infoTemplate = new InfoTemplate();

                infoTemplate.content = strhotspot;

                var graphic = new Graphic(poly_wm, symbol);

                graphic.setInfoTemplate(infoTemplate);

                MyGraphicsLayerGL.add(graphic);

                mapDiv.style.visibility = "visible";

                DivesriLogoImage.style.visibility = "hidden";

             

            }

0 Kudos
1 Solution

Accepted Solutions
thejuskambi
Regular Contributor

you dont need "[". Just use Points1.push([list.d[CMt].EPolyLong, list.d[CMt].EPolyLat]);

doesn't it work?

again in AddPolygonGraphics you dont need to add "[" you are simply converting an array into string.

View solution in original post

4 Replies
thejuskambi
Regular Contributor

The addRing Method in the Polygon takes either a Point[] or Number[][]. whereas, you are providing it with a string value. That might be the cause of the problem. Try below code

Points1= [];

while (CMt <= (list.d.length - 1)){

    ar newPoint = new Point(list.d[CMt].EPolyLong, list.d[CMt].EPolyLat ,new SpatialReference({ wkid: 4326 }));

    gname = list.d[CMt].EPolyGname;

    GDesc = list.d[CMt].EPolyGDesc;

    Points1.push(newPoint);

    CMt = CMt + 1;

};

Or

Points1= [];

while (CMt <= (list.d.length - 1)){

    Point1.push([list.d[CMt].EPolyLong, list.d[CMt].EPolyLat]);

    gname = list.d[CMt].EPolyGname;

    GDesc = list.d[CMt].EPolyGDesc;

    CMt = CMt + 1;

};

JulieBiju
Occasional Contributor

Thank u. Here I tried in the below way. But when I add "[" character and when it reach to AddPolygonGraphics again the same wrong result

Can u help me?

Points1 = [];

while (CMt <= (list.d.length - 1))

{

Points1.push("["+[list.d[CMt].EPolyLong, list.d[CMt].EPolyLat]+"]");

     gname = list.d[CMt].EPolyGname;

    GDesc = list.d[CMt].EPolyGDesc;

     CMt = CMt + 1;

   }

alert(Points1.length);

if(Points1.length>0)

   {

   AddPolygonGraphics(Points1, gname, GDesc);

   Points1 = [];

  }


                             

        

function AddPolygonGraphics(Points1, gname, GDesc) {

                alert(Points1);

                Points1 = "[" + Points1 + "]"

                var polygon1 = new Polygon(new SpatialReference({ wkid: 4326 }));

                 //polygon1.addRing([[58.4245719470205, 23.582511030118049], [58.423799470824214, 23.594545845196812],[58.44002147094627, 23.5743298721676], [58.443798021239232, 23.578735174416867], [58.4245719470205, 23.582511030118049]]);

                polygon1.addRing(Points1);

//                alert(polygon1.rings[0].length);

                // map is in web mercator so transform the geometry 

                var poly_wm = webMercatorUtils.geographicToWebMercator(polygon1);

                var symbol = new SimpleFillSymbol();

                var strhotspot = (gname.trim());

                if (GDesc != "")

                {

                    strhotspot = strhotspot + "," + (GDesc.trim());

                }

                var infoTemplate = new InfoTemplate();

                infoTemplate.content = strhotspot;

                var graphic = new Graphic(poly_wm, symbol);

                graphic.setInfoTemplate(infoTemplate);

                MyGraphicsLayerGL.add(graphic);   

    

            }

0 Kudos
thejuskambi
Regular Contributor

you dont need "[". Just use Points1.push([list.d[CMt].EPolyLong, list.d[CMt].EPolyLat]);

doesn't it work?

again in AddPolygonGraphics you dont need to add "[" you are simply converting an array into string.

View solution in original post

JulieBiju
Occasional Contributor

Yes dear friend,

Works well...Thank u so much....

0 Kudos