Openlayers 3 - 交互和指针移动
Openlayers 3 - Interaction and pointermove
我正在尝试在鼠标悬停在某个功能内时激活交互。
一切正常...问题是如果您缓慢移动鼠标,交互会保持活动状态。
这是 OL3 上的错误,还是我应该换一种方式?
代码:http://jsfiddle.net/gmaq54dm/3/
olMap.on("pointermove", function (e) {
if (e.dragging) {
return;
}
var map = e.map;
console.log(e.pixel);
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
return feature;
});
var hit = (feature) ? true : false;
console.log(hit);
olDraw.setActive(hit);
});
谢谢
我同意@jonatas,这似乎是一个错误。
不过,有一个解决方法似乎可以完成您的工作。
- 避免多行字符串。它使 ol3 更复杂地验证您的鼠标何时位于该功能上。
- 使用
vectorSource.forEachFeatureInExtent(
,然后使用鼠标坐标创建一个小矩形,并在每个方向上增加几米。这将确保您的鼠标 "mbr" 符合功能。
请注意,我使用距鼠标坐标 +-5 米的距离来构建 mbr。你必须调整以满足你的要求。
这是您的应用程序中的错误,而不是 OpenLayers 中的错误。您需要确保仅从矢量图层而不是绘图层中命中检测特征。将 forEachFeatureAtPixel
函数更改为
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
return feature;
}, null, function(layer) {
return layer == vectorLayer
});
最后一个参数添加一个层过滤器以仅命中检测矢量图层上的特征。
已更新,正在运行的 JSFiddle:http://jsfiddle.net/gmaq54dm/4/
我正在尝试在鼠标悬停在某个功能内时激活交互。
一切正常...问题是如果您缓慢移动鼠标,交互会保持活动状态。
这是 OL3 上的错误,还是我应该换一种方式?
代码:http://jsfiddle.net/gmaq54dm/3/
olMap.on("pointermove", function (e) {
if (e.dragging) {
return;
}
var map = e.map;
console.log(e.pixel);
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
return feature;
});
var hit = (feature) ? true : false;
console.log(hit);
olDraw.setActive(hit);
});
谢谢
我同意@jonatas,这似乎是一个错误。
不过,有一个解决方法似乎可以完成您的工作。
- 避免多行字符串。它使 ol3 更复杂地验证您的鼠标何时位于该功能上。
- 使用
vectorSource.forEachFeatureInExtent(
,然后使用鼠标坐标创建一个小矩形,并在每个方向上增加几米。这将确保您的鼠标 "mbr" 符合功能。 请注意,我使用距鼠标坐标 +-5 米的距离来构建 mbr。你必须调整以满足你的要求。
这是您的应用程序中的错误,而不是 OpenLayers 中的错误。您需要确保仅从矢量图层而不是绘图层中命中检测特征。将 forEachFeatureAtPixel
函数更改为
var feature = map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
return feature;
}, null, function(layer) {
return layer == vectorLayer
});
最后一个参数添加一个层过滤器以仅命中检测矢量图层上的特征。
已更新,正在运行的 JSFiddle:http://jsfiddle.net/gmaq54dm/4/