簡體   English   中英

OpenLayers 3:簡單的LineString示例

[英]OpenLayers 3: simple LineString example

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

只需改變這個:

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構造函數接受一個坐標數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM