2016-04-27 13 views
0

,我想在我的OpenLayers創建一些多邊形3的地圖,但得到以下錯誤:Asse田:斷言失敗:座標數組的長度應與步幅

AssertionError: Assertion failed: length of coordinate array should match stride

我使用的代碼如下: :

var geometry = new ol.geom.Polygon([ 
    [10.689697265625, -25.0927734375], 
    [34.595947265625, -20.1708984375], 
    [38.814697265625, -35.6396484375], 
    [13.502197265625, -39.1552734375], 
    [10.689697265625, -25.0927734375] 
], "XY"); 

geometry.transform('EPSG:4326', 'EPSG:3857'); 

var vectorLayer = new ol.layer.Vector({ 
    map: this.map, 
    source: new ol.source.Vector({ 
     features: [new ol.Feature({ 
      geometry: geometry 
     })] 
    }) 
}); 

我在努力尋找解決方案,並在互聯網上找不到的錯誤本身(比的OpenLayers的源代碼等)的任何引用。

我已經找到了解決方案,但是我將其發佈在這裏以供參考,以防將來有人遇到同樣的問題。

那麼,它到底是什麼?

回答

2

多挖後,我已經意識到了多邊形的定義需要額外的組括號:

var geometry = new ol.geom.Polygon([ [ 
    [10.689697265625, -25.0927734375], 
    [34.595947265625, -20.1708984375], 
    [38.814697265625, -35.6396484375], 
    [13.502197265625, -39.1552734375], 
    [10.689697265625, -25.0927734375] 
] ]); 

geometry.transform('EPSG:4326', 'EPSG:3857'); 

var vectorLayer = new ol.layer.Vector({ 
    map: this.map, 
    source: new ol.source.Vector({ 
     features: [new ol.Feature({ 
      geometry: geometry 
     })] 
    }) 
}); 

而這個作品!

這是一個已經終於啓發我的jsfiddle:http://jsfiddle.net/q8s2z/111/

作爲documentation狀態中,座標參數是ol.Coordinate的數組的數組(這也是陣列)。

同樣,MultiPolygon將被定義爲:

var geometry = new ol.geom.MultiPolygon([ [ 
    [10.689697265625, -25.0927734375], 
    [34.595947265625, -20.1708984375], 
    [38.814697265625, -35.6396484375], 
    [13.502197265625, -39.1552734375], 
    [10.689697265625, -25.0927734375] 
], [ 
    [10.689697265625, -25.0927734375], 
    [34.595947265625, -20.1708984375], 
    [38.814697265625, -35.6396484375], 
    [13.502197265625, -39.1552734375], 
    [10.689697265625, -25.0927734375] 
] ]);