gpt4 book ai didi

javascript - 拖动功能后关闭弹出叠加层

转载 作者:行者123 更新时间:2023-12-03 07:46:59 26 4
gpt4 key购买 nike

当我将鼠标拖动到 map 中的矢量要素上时,会出现一个弹出窗口。此弹出窗口基于两个 OpenLayers 示例: OpenLayers example #1OpenLayers example #2

我希望当鼠标不再位于该功能上时弹出窗口自动关闭。知道如何才能实现这一目标吗?

实际上,这是我的代码:

var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
var overlay = new ol.Overlay(/** @type {olx.OverlayOptions} */ ({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
}));

closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};

var highlightStyleCache = {};

var featureOverlay = new ol.layer.Vector({
source: new ol.source.Vector(),
map: olMap,
style: function(feature, resolution) {
var text = resolution < 5000 ? feature.get('name') : '';
if (!highlightStyleCache[text]) {
highlightStyleCache[text] = new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#f00',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.1)'
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
text: text,
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#f00',
width: 3
})
})
});
}
return highlightStyleCache[text];
}
});

var highlight;
var displayFeatureInfo = function(pixel,coordinate) {

var feature = olMap.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
});

// var info = document.getElementById('info');
if (feature) {
content.innerHTML = 'Feature value:\n' + feature.get('ZLEVEL');
overlay.setPosition(coordinate);
} else {
// info.innerHTML = '&nbsp;';
}

if (feature !== highlight) {
if (highlight) {
featureOverlay.getSource().removeFeature(highlight);
}
if (feature) {
featureOverlay.getSource().addFeature(feature);
}
highlight = feature;
}

};

var olMap = new ol.Map({
layers: [group, group3, group2],
overlays: [overlay],
target: "map",
view: new ol.View({
center: ol.proj.transform([-71.16237,48.42432], "EPSG:4326", "EPSG:3857"),
zoom: 14,
projection: "EPSG:3857"
})
});

olMap.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
var pixel = olMap.getEventPixel(evt.originalEvent);
var coordinate = evt.coordinate;
displayFeatureInfo(pixel,coordinate);
});

最佳答案

通过拖动出现弹出窗口是否有特殊原因,但我建议使用带有指针移动的选择交互。

var selectinteraction = new ol.interaction.Select({
condition: ol.events.condition.pointerMove,//maybe ther is a drag?
layers: [**your vectorlayer**]
});

创建 selectinteraction 后,您可以 Hook 事件。请注意,以下只是伪代码。

selectinteraction.on('select', function (e) {
e.target.getFeatures().forEach(function (feature, index) {
//if xou eant to show some selected feature stuff
closer.setPopupInformation(feature);
closer.show();
});
//if select is empty as your mouse isn't on a feature element, close the popup
if (e.target.getFeatures().getLength() === 0) {
closer.hide();
}
});

关于javascript - 拖动功能后关闭弹出叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com