【发布时间】:2018-04-04 12:30:26
【问题描述】:
在将鼠标悬停在 geojson layer/s 上方时快速(我相信对你们中的一些人来说是一个简单的)关于光标样式的问题。
所以,我有一个剪辑层,用于在 wms 层周围创建蒙版,另一个代表一些行政区域。 如下图所示
当我将鼠标悬停在行政区域上方时,我想要更改光标的样式,但似乎我遗漏了一些东西。
我正在尝试使用此代码块将仅管理边界层隔离到层:
map.on('pointermove', function(e) {
if (e.dragging) return;
var pixel = e.map.getEventPixel(e.originalEvent);
var hit = e.map.forEachFeatureAtPixel(pixel, function(feature, layer) {
return vectorJLS.get('layer_name') === 'jls';
});
e.map.getTargetElement().style.cursor = hit ? 'pointer' : '';
});
更新
虽然 JGH 稍微调整了代码,但它仍然不起作用。我检测到问题出在我用于蒙版剪辑的图层中,删除后,JGH 提供的代码可以正常工作。
这是我用于蒙版剪辑的代码
var clipLayer = new ol.layer.Image({
source: new ol.source.ImageVector({
source: new ol.source.Vector({
url: 'geojson/clip_wgs.geojson',
format: new ol.format.GeoJSON()
}),
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'black'
})
})
})
});
clipLayer.on('postcompose', function(e) {
e.context.globalCompositeOperation = 'source-over';
});
clipLayer.on('precompose', function(e) {
e.context.globalCompositeOperation = 'destination-in';
});
clipLayer.setMap(map);
在更改光标样式时是否可以以某种方式忽略剪辑层,还是应该采取其他方法?
更新 - 2
我已经稍微调整了代码,但在 clipedLayer 开启时仍然没有任何成功。
map.on('pointermove', function(e) {
if (e.dragging) return;
var pixel = e.map.getEventPixel(e.originalEvent);
// initialize the hit variable to false (not found)
var hit = map.hasFeatureAtPixel(e.pixel, {
layerFilter: function(layer) {
return vectorJLS.get('layer_name') === 'jls';
}
});
console.log(hit)
});
如果我可以添加有趣的问题
【问题讨论】:
-
如所写,它取决于层顺序。找到感兴趣的图层后,您需要退出函数
-
@JGH 你能详细说明一下吗
标签: javascript canvas openlayers openlayers-3