Search code examples
lineopenlayerspoint

Draw line between two points using OpenLayers


I have two points having lolLat as 0,10 and 30,0

Now to draw a marker at this point i use this transform while generating marker for it

lonLat.transform(
                 new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                 map.getProjectionObject() // to Spherical Mercator Projection
             )

How can i draw line between them is there any way to do that in openlayers,i have tried doing it with linestring in vector layer but it doesn't seems to be working for me.


Solution

  • For example:

    map = new OpenLayers.Map();
    
    var start_point = new OpenLayers.Geometry.Point(0,10);
    var end_point = new OpenLayers.Geometry.Point(30,0);
    
    var vector = new OpenLayers.Layer.Vector();
    vector.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString([start_point, end_point]))]);
    map.addLayers([vector]);
    

    Live demo