在开放层3中绘制线

drawing lines in open layers 3

本文关键字:绘制      更新时间:2023-09-26

我知道已经有很多这样的帖子了,但我已经尝试了很多,但他们似乎并没有解决我的问题。

我正试图用线串在地图上画一条线,但无论我做什么,它都画不成线。这是我的代码:

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
});

如果我在没有转换的情况下创建了行字符串,那么它确实在0,0处显示了一个点,但我不认为它无法读取我的坐标,因为如果我将其留空,那么就不会显示任何点,因此它不能使用默认值。

我对javascript和OL还很陌生,所以我目前的示例项目是创建一个测量应用程序,人们可以在其中测量两点并在它们之间画一条线。回答时请记住这一点。

注意一些变化:

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/

我把coords改了,原来有点怪|错了。