Javascript OpenLayers 3:简单的 LineString 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30888181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:53:44  来源:igfitidea点击:

OpenLayers 3: simple LineString example

javascriptvectoropenlayers-3

提问by ThriceGood

i'm new to OpenLayers and i am looking for some help drawing lines on a map, i've been trying various things from various different posts about drawing LineStrings but i can't get it to work! I just need to figure out how to draw a line between to coordinates.

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

heres some code that i tried but didn't work:

这是我尝试过但没有用的一些代码:

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

i also tried this but to no avail:

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

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

can someone point me in the right direction? or tell me where i am going wrong?

有人可以指出我正确的方向吗?或者告诉我哪里出错了?

EDIT: thanks to Jonatas, the working code looks like this:

编辑:感谢 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);

采纳答案by Jonatas Walker

Just change this:

只需改变这个:

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

To:

到:

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

The ol.geom.LineStringconstructor accept an array of coordinates.

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