我通过使用LineString几何来使用openlayer v5和样式功能,而没有任何不同的样式。 样式功能代码:
applyStyle(feature,resolution){
var styleArray = [
new ol.style.Style({ // feature style
stroke: new ol.style.Stroke({
color: 'green',
width: 6,
lineCap: 'square'
}),
text: new Text({
text: 'gas',
textAlign: 'start',
offsetX: 1,
offsetY: 1,
fill: new Fill({
color: 'white'
})
})
}),
new ol.style.Style({ //feature sub style geometry
geometry: function (feature) {
var line = feature.getGeometry();
var coords = [];
line.forEachSegment(function (from, to) {
// for each segment calculate a parallel segment with a
// distance of 4 pixels
var angle = Math.atan2(to[1] - from[1], to[0] - from[0]);
var dist = 5 * resolution;
var newFrom = [
Math.sin(angle) * dist + from[0],
-Math.cos(angle) * dist + from[1]
];
var newTo = [
Math.sin(angle) * dist + to[0],
-Math.cos(angle) * dist + to[1]
];
coords.push(newFrom);
coords.push(newTo);
});
return new ol.geom.LineString(coords);
},
stroke: new ol.style.Stroke({
color: 'red',
width: 5,
lineCap: 'square'
}),
text: new Text({
text: 'redtext',
textAlign: 'start',
offsetX: 1,
offsetY: 1,
fill: new Fill({
color: 'white'
})
})
})
];
feature.setStyle(styleArray);
}
this.map.on('singleclick', function (evt: any) {
console.log("single click");
var features: any = self.map.getFeaturesAtPixel(evt.pixel);
features.forEach(feature => {
var styles = feature.getStyle();
delete styles[0]; //deleting feature style from array only
console.log();
styles.forEach(style => {
console.log();
var line: any = style.getGeometry();
var sss = style.getLineString(); //not working
var lineString = line.getLineString(); //not working
var geometry = style.getCoordinates(); //not working
console.log();
})
});
});
在上面有关地图单击事件的代码中,我尝试了getGeometry(),但未返回几何或坐标。
任何帮助单击即可获得子样式几何的方法。