简体   繁体   English

OpenLayers 3:简单的LineString示例

[英]OpenLayers 3: simple LineString example

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! 我是OpenLayers的新手,我正在寻找一些帮助在地图上绘制线条,我一直在尝试各种不同的帖子关于绘制LineStrings但我无法让它工作! I just need to figure out how to draw a line between to coordinates. 我只需要弄清楚如何在坐标之间画一条线。

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

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.LineString constructor accept an array of coordinates. ol.geom.LineString构造函数接受一个坐标数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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