2016-04-27 14 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] 
] ]);