[GIS] How to draw a line between markers in openlayers

javascriptopenlayers-2

how to draw the line between markers, coordinates of markers undertake from a DB. whether it is possible to draw in general lines between markers, I saw examples only with points.

    markers = new OpenLayers.Layer.Markers("Marcadores");
    map.addLayer(markers);

    if (b == 2) 
    {           

            //=================  =========
            var size = new OpenLayers.Size(30, 30);
            var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);

            var location = new OpenLayers.LonLat(kor_1,kor_2).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
            var icon = new OpenLayers.Icon('http://icons.iconarchive.com/icons/icons-land/vista-map-markers/256/Map-Marker-Ball-Pink-icon.png', size, offset);
            markers.addMarker(new OpenLayers.Marker(location, icon.clone()));
            var line = new OpenLayers.Geometry.LineString([kor_1, kor_2]);
        //================= ==============

    }

Best Answer

You need to add a vector layer, and then add a feature to it. See the following code:

var startPt=new OpenLayers.Geometry.Point( 2, 45);
var endPt=new OpenLayers.Geometry.Point(7,55);

//make the line:
var line=new OpenLayers.Geometry.LineString([startPt, endPt]);

//style
var style={strokeColor:"#0500bd", strokeWidth:3};
//make vector 
var fea=new OpenLayers.Feature.Vector(line, {}, style);

//make vectorLayer
var vec= new OpenLayers.Layer.Vector();

//add the feature
vec.addFeatures([fea]);

//add to map
map.addLayer(vec);

You can see a working sample here: http://jsfiddle.net/devdatta/pwuVz/2/