OpenLayers 3:简单的LineString示例 [英] OpenLayers 3: simple LineString example

查看:3027
本文介绍了OpenLayers 3:简单的LineString示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

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

thanks to Jonatas, the working code looks like this:

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


推荐答案

只需更改此内容:

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 构造函数接受一个坐标数组。

The ol.geom.LineString constructor accept an array of coordinates.

这篇关于OpenLayers 3:简单的LineString示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆