Openlayers 如何在地图上获取要素子几何体样式坐标单击事件
我正在使用openlayer v5和样式功能,通过使用线串镶嵌术,没有不同的样式。 样式设置功能部件代码:Openlayers 如何在地图上获取要素子几何体样式坐标单击事件,openlayers,openlayers-5,Openlayers,Openlayers 5,我正在使用openlayer v5和样式功能,通过使用线串镶嵌术,没有不同的样式。 样式设置功能部件代码: applyStyle(feature,resolution){ var styleArray = [ new ol.style.Style({ // feature style stroke: new ol.style.Stroke({ color: 'green', width: 6, lineCap
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(),但它并没有返回几何体或坐标
单击即可获得子样式几何体的任何帮助。在调用
style之前尝试控制台style。getGeometry
style的控制台是{“填充”:null,“图像”:null,“渲染器”:null,“笔划”:{“颜色”:“#85ACBD”,“线帽”:“正方形”,“线划线”:null,“宽度”:16},“文本”:{“文本”:“水”,“文本对齐”:“开始”,“填充”:{“颜色”:“white”},“maxAngle”}0.785398163397483,“placement”}“point”,“overflow”}false,“stroke”}null,“offsetX”}1,“offsetY”}1,“backgroundFill”}null,“backgroundStroke”}null,“padding”}这里没有几何体。所以我怎么知道是哪种样式,因为功能包含多个样式。那么有没有办法知道功能的哪种样式?如果几何体选项是一个函数getGeometry()
返回函数,那么您需要调用它来获取几何体style.getGeometry()(功能)
每次单击时也会删除第一个样式下次单击时将删除第二个样式。请使用style.slice(1)。forEach
避免这种情况。