Drawing line with polyline.addPath

2980
3
07-23-2010 06:47 AM
MikeKillion
New Contributor II
Hello,
I'm trying to draw a line on the map using the addPath method. Here's the code snippet (using v1.6):
var line = new esri.geometry.Polyline(new esri.SpatialReference( {wkid:102100} ));
var path = new Array();
   
for (i=0; i<xPoints.length; i++)
{
    coords = '[' + xPoints + ',' + yPoints + ']';
    path.push(coords);
}
   
alert(path);
   
line.addPath(path);

alert(line.paths);


The first alert shows the path formatted like this:
[-10993319,4632566],[-10907471,4612961]
which seems correct to me.

However, the second alert (after the path is added to the polyline) only shows 3 commas (,,,) so the path doesn't get added and the line doesn't draw.

Any ideas appreciated.
Mike
0 Kudos
3 Replies
KellyHutchins
Esri Frequent Contributor
It looks like you are creating a string that looks like an array from your x,y values. You should be able to do something like this instead: 

         
var coords = [];
var polyline = new esri.geometry.Polyline(new esri.SpatialReference({wkid:102100}));
    for (i=0;i < xPoints.length -1;i++){
        coords.push([xPoints, yPoints]);           
     }
polyline.addPath(coords);
0 Kudos
MikeKillion
New Contributor II
Thanks Kelly, that did it.
0 Kudos
deleted-user-0W0-oLHxDjCX
New Contributor III

It is possible to add a midpoint without having the coordinate (only having the first and last point)?

0 Kudos