我有一个绘图交互来绘制点。只有在捕捉到其他源的要素边界时才允许绘制这些点。现在,如果其他特征边界被捕捉,我的叠加特征必须改变他自己的风格。
var snapCondition = function(evt){
var features = [];
mapEntry.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
if(layer != null && layer.get('name') === 'specialLayerName') {
features.push(feature);
}
});
if(features.length === 1 ) {
return checkBoundary(features[0], evt.coordinate);
} else {
return false;
};
};
var checkBoundary = function(feature, coords) {
var geom = feature.getGeometry();
var closestPoint = geom.getClosestPoint(coords);
console.log(closestPoint);
console.log(coords)
if((closestPoint[0] === coords[0]) && (closestPoint[1] === coords[1])) {
return true;
}
return false;
}
var drawBuildingEntry = new ol.interaction.Draw({
source: buildingEntrySource,
type: 'Point',
condition: snapCondition,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: entryDrawColor,
}),
stroke: new ol.style.Stroke({
color: 'white',
width: 3
})
})
})
});
上面的代码可以正常工作,但是我通过改变样式遇到了问题。我测试了以下代码:
mapEntry.on("pointermove", function (event) {
var features = [];
mapEntry.forEachFeatureAtPixel(event.pixel, function(feature, layer) {
if(layer != null && layer.get('name') === 'specialLayerName') {
features.push(feature);
}
});
if(features.length === 1 ) {
console.log(checkBoundary(features[0], event.coordinate));
if(checkBoundary(features[0], event.coordinate) === true) {
entryDrawColor = '#40FF00'
} else {
entryDrawColor = '#FF0000';
};
} else {
entryDrawColor = '#FF0000';
};});
checkBoundary 的console.log 甚至是假的,因为事件坐标是覆盖元素被捕捉之前的坐标。最好的问候蒂姆