Remove tooltip when mouse leaves map or is over control is examples

This commit is contained in:
Maximilian Krög
2022-08-21 11:51:06 +02:00
parent 62914bb3df
commit a10c93d331
2 changed files with 26 additions and 12 deletions

View File

@@ -93,10 +93,12 @@ const tooltip = new bootstrap.Tooltip(info, {
});
let currentFeature;
const displayFeatureInfo = function (pixel) {
const feature = map.forEachFeatureAtPixel(pixel, function (feature) {
return feature;
});
const displayFeatureInfo = function (pixel, target) {
const feature = target.closest('.ol-control')
? undefined
: map.forEachFeatureAtPixel(pixel, function (feature) {
return feature;
});
if (feature) {
info.style.left = pixel[0] + 'px';
info.style.top = pixel[1] + 'px';
@@ -121,9 +123,14 @@ map.on('pointermove', function (evt) {
return;
}
const pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel);
displayFeatureInfo(pixel, evt.originalEvent.target);
});
map.on('click', function (evt) {
displayFeatureInfo(evt.pixel);
displayFeatureInfo(evt.pixel, evt.originalEvent.target);
});
map.getTargetElement().addEventListener('pointerleave', function () {
tooltip.hide();
currentFeature = undefined;
});