简体   繁体   English

在openlayers 4中使用外部超链接正确显示弹出窗口

[英]show popups correctly with external hyperlinks in openlayers 4

I have an openlayers map that loads a couple of kml files containing about 120 polygon placemarks each. 我有一个openlayers地图,它加载了几个kml文件,每个文件包含大约120个多边形地标。 As they're too many to show a popup for each, I had to create an outside-map menu, so the user can click on any one of these features and see it's info / location. 由于它们太多了,无法显示每个弹出窗口,因此我必须创建一个外部地图菜单,以便用户可以单击这些功能中的任何一个并查看其信息/位置。

I use this function to create the outside-map menu, containing all the features: 我使用此函数来创建包含所有功能的外部地图菜单:

        vEnergeticos.getSource().on('change', function(evt){
            var source = evt.target;
            if (source.getState() === 'ready') {
                var energeticos = source.getFeatures();
                for (var i in energeticos) {
                    var figura = energeticos[i].getGeometry().getExtent();
                    var myCenter = ol.extent.getCenter(figura);
                    $("#containerLeft").append("<a href=javascript:showMenuPopup(" + myCenter + "," + energeticos[i].get('ID') + ")>" + energeticos[i].get('name') + "</a><br>");
                }
            }
        });

and then when the user clicks on any of these options, this function is called: 然后,当用户单击以下任何一个选项时,此函数将被调用:

        function showMenuPopup(xx, yy, theID){
            var myPixel = map.getPixelFromCoordinate([xx, yy]);
            var elNombre = "";
            var laDescripcion = "";
            map.forEachFeatureAtPixel(myPixel, function(feature, layer) {
                if (feature.get('ID') == theID){
                    elNombre = feature.get('name');
                    laDescripcion = feature.get('description');
                }
            });

            popupTitle.innerHTML = elNombre;
            popupContent.innerHTML = laDescripcion;
            overlay.setPosition([xx,yy]);
        }

This works in some situations, however, when the selected feature is outside of the current map view, the map relocates successfully (overlay.setPosition([xx,yy]);), the popup is shown, but the popup is empty . 这在某些情况下可行,但是,当所选要素不在当前地图视图之外时,地图将成功重定位(overlay.setPosition([xx,yy]);),显示弹出窗口, 但弹出窗口为空 If the feature is visible when the user clicks from the left menu, then the popup is shown correctly. 如果用户从左侧菜单中单击时该功能可见,则将正确显示弹出窗口。

Just to be clear enough, imagine you're seeing a map where you can see part of Europe, and then you click on some item located in Canada (using the off-map menu), you'll see the map relocates in Canada, but the popup that is shown is empty. 为了清楚起见,假设您看到的是可以看到欧洲部分地区的地图,然后单击位于加拿大的某些商品(使用离线地图菜单),您会看到地图在加拿大的重新定位,但是显示的弹出框为空。 If you click again on that very same off-map link, or any other feature that is visible at that location/zoom view, then the popup is shown correctly. 如果再次单击该非常相同的非地图链接,或在该位置/缩放视图上可见的任何其他功能,则将正确显示弹出窗口。

I tried to use the "moveend (ol.MapEvent)" in order to fix this, so the popup was loaded after the map is relocated, but it didn't work for me. 我试图使用“ moveend(ol.MapEvent)”来解决此问题,因此在重新定位地图后加载了弹出窗口,但它对我不起作用。 The moveend event is called before the map starts to move using overlay.setPosition([xx,yy]), and I haven't been able to find some other "after-relocation" event that I could use. 在移动地图开始使用overlay.setPosition([xx,yy])之前,将调用moveend事件,但我找不到其他可以使用的“重定位后”事件。

I've been struggling with this for many days now, so any help will be really appreciated. 我已经为此苦苦挣扎了很多天,因此我们将不胜感激。

Regards!! 问候!!

The problem is that the features outside of the current map view are not "AtPixel", so you won't catch them with map.forEachFeatureAtPixel. 问题在于当前地图视图之外的要素不是“ AtPixel”,因此您不会使用map.forEachFeatureAtPixel来捕获它们。

I suggest you to avoid passing coordinates to showMenuPopup: you just need the feature id, than you can retrieve the feature's coordinates inside showMenuPopup. 我建议您避免将坐标传递给showMenuPopup:您只需要要素ID,而不是可以在showMenuPopup中检索要素的坐标。

$("#containerLeft").append("<a href=javascript:showMenuPopup('" + energeticos[i].getId() + "')>" + energeticos[i].get('name') + "</a><br>");

Then 然后

function showMenuPopup(featureId){
    var feature = vEnergeticos.getSource().getFeatureById(featureId);
    var elNombre = feature.get('name');
    var laDescripcion = feature.get('description');
    var figura = feature.getGeometry().getExtent();
    var myCenter = ol.extent.getCenter(figura);

    popupTitle.innerHTML = elNombre;
    popupContent.innerHTML = laDescripcion;
    overlay.setPosition(myCenter);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM