Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
RPB Avatar asked Mar 19 '12 05:03

RPB


People also ask

How do you draw lines in Openlayers?

Select a geometry type from the dropdown above to start drawing. To finish drawing, click the last point. To activate freehand drawing for lines, polygons, and circles, hold the Shift key. To remove the last point of a line or polygon, press "Undo".

How do you remove layers from Openlayers?

filter(layer => layer. get('name') === 'Marker') . forEach(layer => map. removeLayer(layer));


1 Answers

In OpenLayers version 3.3, you can do this as

var points = [ [-89.8802, 32.5804], [-95.04286, 46.9235] ];

for (var i = 0; i < points.length; i++) {
    points[i] = ol.proj.transform(points[i], 'EPSG:4326', 'EPSG:3857');
}

var featureLine = new ol.Feature({
    geometry: new ol.geom.LineString(points)
});

var vectorLine = new ol.source.Vector({});
vectorLine.addFeature(featureLine);

var vectorLineLayer = new ol.layer.Vector({
    source: vectorLine,
    style: new ol.style.Style({
        fill: new ol.style.Fill({ color: '#00FF00', weight: 4 }),
        stroke: new ol.style.Stroke({ color: '#00FF00', width: 2 })
    })
});

Then add layer to map

map.addLayer(vectorLineLayer);
like image 97
Pratik Goenka Avatar answered Sep 21 '22 13:09

Pratik Goenka