简体   繁体   中英

drawing lines in open layers 3

I know that there are a lot of posts like this already but I have tried a lot of them and they don't seem to be fixing my problem.

I am trying to draw a line on a map using linestring but no matter what I do, it does not draw a line. Here is my code:

var coords = [[78.65, -32.65], [15.65, -98.65]];

var lineStyle = new ol.style.Style({
    stroke: new ol.style.Stroke(({
        width: 10
    }))
});

var layerLines = new ol.layer.Vector({
    style: lineStyle,
    source: new ol.source.Vector({
        features: [new ol.Feature({
            geometry: new ol.geom.LineString(coords, 'EPSG:4326',   'EPSG:3857'),
            name: 'Line'
        })]
    }),
});

var map = new ol.Map({
    layers: [
        mainLayer,
        vectorLayer,
        layerLines
    ],
    projection: "EPSG:3857",
    target: 'map',
    view: view
});

If I create the linestring without the transformations then it does display a single dot at 0,0 but I do not think it cannot read my coordinates because if I leave it blank then no dot appears so it can't be using a default value.

I am pretty new to javascript and OL so my current sample project is to create a measuring app where people can measure two points and have a line drawn between it. Please bear this in mind when answering.

Note some changes:

var coords = [[-65.65, 10.10], [13, 18]];
var lineString = new ol.geom.LineString(coords);
// transform to EPSG:3857
lineString.transform('EPSG:4326', 'EPSG:3857');

// create the feature
var feature = new ol.Feature({
    geometry: lineString,
    name: 'Line'
});

http://jsfiddle.net/jonataswalker/7cf5egm2/

I changed the coords , the original are a bit strange|wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM