我对只有四个顶点的线有同样的问题。我将 ol.SIMPLIFY_TOLERANCE 的数字更改为 -1,并且该功能的渲染没有任何变化。如果我调用geometry.getSimplifiedGeometry(0),我会取回所有四个顶点。但是在渲染时,只返回两个顶点。我想知道是否需要更改其他内容?多边形似乎渲染得很好。我是 OpenLayers 3 的新手,所以我确信有更好的方法来解决这个问题。
我可以使用样式功能使线条正确显示。我在下面放了一个我选择的风格的样本。我还为矢量图层创建了一个标准样式函数。如果我没有将样式函数添加到选择交互中,我的特征将从具有 4 个顶点的线跳到只有起点和终点的线。
var selectStyleFunction = function (feature, resolution) {
var styles = [];
var white = [255, 255, 255, 1];
var blue = [0, 153, 255, 1];
var width = 3;
var geometry = feature.getGeometry();
var type = geometry.getType();
if (type == 'Point') {
styles.push(new ol.style.Style({
image: new ol.style.Circle({
radius: width * 2,
fill: new ol.style.Fill({
color: blue
}),
stroke: new ol.style.Stroke({
color: white,
width: width / 2
})
}),
zIndex: Infinity
}));
}
if (type == 'LineString') {
geometry.forEachSegment(function (start, end) {
styles.push(new ol.style.Style({
geometry: new ol.geom.LineString([start, end]),
stroke: new ol.style.Stroke({
color: white,
width: width + 2
})
}));
styles.push(new ol.style.Style({
geometry: new ol.geom.LineString([start, end]),
stroke: new ol.style.Stroke({
color: blue,
width: width
})
}));
});
}
if (type == 'Polygon') {
styles.push(new ol.style.Style({
fill: new ol.style.Fill({
color: [255, 255, 255, .5]
})
}));
styles.push(new ol.style.Style({
stroke: new ol.style.Stroke({
color: white,
width: width + 2
})
}));
styles.push(new ol.style.Style({
stroke: new ol.style.Stroke({
color: blue,
width: width
})
}));
}
return styles;
}
我用于 LineString 功能的另一种样式,我添加到用于矢量图层的样式函数中。这个向每个顶点添加点,并且基于 OpenLayers 示例站点上的多边形示例:
if (type == horizontal') {
var coords = geometry.getCoordinates();
geometry.forEachSegment(function (start, end) {
styles.push(new ol.style.Style({
geometry: new ol.geom.LineString([start, end]),
stroke: new ol.style.Stroke({
color: [0, 128, 0, .9],
width: width + 2
}),
zIndex: 9000
}));
});
styles.push(new ol.style.Style({
image: new ol.style.Circle({
radius: width + 2,
fill: new ol.style.Fill({
color: [0, 128, 0, 2, 1]
}),
stroke: new ol.style.Stroke({
color: [255, 255, 255, 0.75],
width: width
})
}),
geometry: function () {
return new ol.geom.MultiPoint(coords);
},
zIndex: 9000
}));
}