6 Oct 2010 07:22

## Re: [OpenLayers-Users] Re: add an arrow to line feature

```ok this kinda worked for me...for putting an arrow on a line, now this
is a line..not a polyline.

I made this function little function
--------------------------
function getAngleofLine(pointList) //calculate angle for screen
{

// point list is an array of OpenLayers.Geometry.Point having two
points of a line

var startPt=map.getPixelFromLonLat(new
OpenLayers.LonLat(pointList[0].x,pointList[0].y));
var endPt=map.getPixelFromLonLat(new
OpenLayers.LonLat(pointList[1].x,pointList[1].y));
//in the above line I think it would work too if we use Coordinates
instead of Pixels, but I used pixel cause its easy to do the math with

var dy=endPt.y - startPt.y;
var dx=endPt.x - startPt.x;

var angle=Math.atan(dy/dx) / (Math.PI/180); //convert to degrees

if(dx<0) // adjustment in angle for line moving to bottom
angle-=180; // switch direction..if I dont do this.. the traigle will
point in other direction in certain cases

return angle;
}

..how to use

///////////

var line=new OpenLayers.Geometry.LineString(pointList); // line

var angle=getAngleofLine(pointList);

if(isNaN(angle))
{
var shape="circle";
angle=0; // when two consecutive records are at same
position..ther wil be no angle
var pt= new
OpenLayers.Geometry.Point(pointList[0].x,pointList[0].y); // point for
arow
}
else{
var shape="triangle";
angle-=90; //<--well I had to do this.. it just worked for
me..I would appreciate a feedback on this
var arrowX=pointList[1].x+((pointList[0].x-pointList[1].x)*0.95);
var arrowY=pointList[1].y+((pointList[0].y-pointList[1].y)*0.95);
var pt= new OpenLayers.Geometry.Point(arrowX,arrowY); //
point for arow
}

var gc=new OpenLayers.Geometry.Collection([line,pt]); // collection

var lineFeature = new OpenLayers.Feature.Vector(gc,null, //
geom,data,style
{
strokeColor: "#4682B4",
//http://gucky.uni-muenster.de/cgi-bin/rgbtab-en
fillColor: "#00688B",
strokeWidth: 2,
strokeOpacity : 0.8,
graphicName :shape,
rotation :angle
}
);

On Sun, Oct 3, 2010 at 3:30 AM, Imran Rajjad <rajjad@...> wrote:
> geometry having a line and point.. set the shape of point to traigle
> and calculate the rotation using simple atan(dy/dx). I will share the
> code on Monday with you
>
> regards,
> Imran
>
> On Fri, Oct 1, 2010 at 2:19 PM, Gagan Bansal
<gaganbansal123@...> wrote:
>>
>> Hi,
>>
>> I was also looking for this feature, i.e. showing the line direction. So
>> based on Sveen suggestion I have written this example
>>
>> http://gaganb.wordpress.com/2010/09/30/draw-line-direction-on-openlayers-feature/
>>
>> Regards,
>> Gagan
>> --
>> View this message in context: http://osgeo-org.1803224.n2.nabble.com/add-an-arrow-to-line-feature-tp5234002p5590646.html
>> Sent from the OpenLayers Users mailing list archive at Nabble.com.
>> _______________________________________________
>> Users mailing list
>> Users@...
>> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>>
>
>
>
> --
> I.R
>

--

--
I.R
```

Gmane