环境
Openlayers v4.6.5
背景
在做polygon闪烁效果时,发现只有其中一条边闪烁,面要素其他几边都没有效果。
最后发现是犯了个低级错误:在添加Polygon时坐标串没有闭合导致(即firstCoordinate和lastCoordinate不一样)
错误坐标串:[[12976694.29785, 4743563.56400], [12908907.75571, 4778074.42433], [12903907.75571, 4708074.42433]]
正确坐标串:[[12976694.29785, 4743563.56400], [12908907.75571, 4778074.42433], [12903907.75571, 4708074.42433],[12976694.29785, 4743563.56400]]
参看官方API解释:
解决方案
虽然坐标串不闭合,也能绘制成功;但在后续应用中发现,不规范的传参会导致功能不正常
// 错误!
//coords = [[12976694.29785, 4743563.56400], [12908907.75571, 4778074.42433], [12903907.75571, 4708074.42433]]
// 正确
coords = [[12976694.29785, 4743563.56400], [12908907.75571, 4778074.42433], [12903907.75571, 4708074.42433],[12976694.29785, 4743563.56400]]
let featureTmp = new ol.Feature({
geometry: new ol.geom.Polygon([coords]),
attributes: null
});
pulseLayer.getSource().addFeature(featureTmp);