在开放层中绘制线条3

时间:2015-11-17 12:46:46

标签: javascript openlayers-3

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

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

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的新手,所以我目前的示例项目是创建一个测量应用程序,人们可以测量两个点,并在它之间画一条线。回答时请记住这一点。

1 个答案:

答案 0 :(得分:2)

请注意一些变化:

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,原来有点奇怪。

相关问题