OpenLayers 3:简单的LineString示例

时间:2015-06-17 09:54:47

标签: javascript vector openlayers-3

我是OpenLayers的新手,我正在寻找一些帮助在地图上绘制线条,我一直在尝试各种不同的帖子关于绘制LineStrings但我无法让它工作!我只需要弄清楚如何在坐标之间画一条线。

继承了我试过但没有用的一些代码:

var points = [
    new ol.geom.Point([78.65, -32.65]),
    new ol.geom.Point([-98.65, 12.65])
  ];

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

var sourceLine = new ol.source.Vector({
    features: [featureLine]
  });

var vectorLine = new ol.layer.Vector({
    source: sourceLine
  });

map.addLayer(vectorLine);

我也试过这个,但无济于事:

var layerLine = new ol.layer.Vector({
      source: new ol.source.Vector({
          features: [new ol.Feature({
              geometry: new ol.geom.LineString(points, 'XY'),
              name: 'Line'
          })]
      }),
  });

map.addLayer(vectorLine);
有人能指出我正确的方向吗?或者告诉我哪里出错了?

编辑:感谢Jonatas,工作代码如下:

  var coordinates = [[78.65, -32.65], [-98.65, 12.65]]; 

  var layerLines = new ol.layer.Vector({
      source: new ol.source.Vector({
          features: [new ol.Feature({
              geometry: new ol.geom.LineString(coordinates),
              name: 'Line'
          })]
      }),
  });

  map.addLayer(layerLines);

1 个答案:

答案 0 :(得分:11)

只需改变一下:

var points = [
    new ol.geom.Point([78.65, -32.65]),
    new ol.geom.Point([-98.65, 12.65])
];

要:

var points = [
    [78.65, -32.65], [-98.65, 12.65]
];

ol.geom.LineString构造函数接受一个坐标数组。