简体   繁体   English

负载标记弹出窗口上的OpenLayers

[英]OpenLayers on Load Markers Popup

I'm using OpenLayers to view a map and I'm having an issue with the marker's popup. 我正在使用OpenLayers查看地图,但标记的弹出窗口有问题。 When the Markers are loaded, I'm assigning them two events the moouseover and mouseout but when any of the markers are triggered with these events only the first created marker's popup is shown, even when I mouseover other markers. 加载标记时,我为它们分配了moouseover和mouseout两个事件,但是当这些事件触发任何标记时,即使我将鼠标悬停在其他标记上,也只会显示第一个创建的标记的弹出窗口。 Its like I'm only creating these events for the first marker and not for all of them.. Any ideas? 就像我只为第一个标记创建这些事件,而不是为所有标记创建事件。有什么想法吗? Thanks 谢谢

var listMarkers = getMarkers();

        for (var i = 0; i < listMarkers.length; i++) {
            var size = new OpenLayers.Size(21, 25);
            var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
            var icon;
            if (listMarkers[i].Icon.trim() === "red") {
                icon = new OpenLayers.Icon
                ('http://www.openlayers.org/dev/img/marker.png', size, offset);
            }
            else {
                icon = new OpenLayers.Icon
                ('http://www.openlayers.org/dev/img/marker-' + listMarkers[i].Icon.trim() + '.png', size, offset);
            }

            var mark = new OpenLayers.Marker(new OpenLayers.LonLat(listMarkers[i].Longitude,
                listMarkers[i].Latitude).transform(new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject()), icon);

            //here add mouseover event
            mark.events.register('mouseover', mark, function (evt) {
                popup = new OpenLayers.Popup.FramedCloud("Popup",
                    new OpenLayers.LonLat(listMarkers[i].Longitude,
                listMarkers[i].Latitude).transform(new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject()),
                    null,
                    '<div><b>' + listMarkers[i].Title + '</b><br/>' + listMarkers[i].Description + '</div>',
                    null,
                    false);
                map.addPopup(popup);
            });
            //here add mouseout event
            mark.events.register('mouseout', mark, function (evt) { popup.hide(); });
            markers.addMarker(mark);
        }

In the mouseover event while creating popup you're referring to listMarkers[i] , which in javascript scope would remember last value of given variable i, so for every popup it would get information from listMarkers[listMarkers.length-1] . 在创建弹出窗口时发生的mouseover事件中,您引用的是listMarkers[i] ,它在javascript范围内将记住给定变量i的最后一个值,因此,对于每个弹出窗口,它将从listMarkers[listMarkers.length-1]获取信息。 To fix this, add details (Title, Latitude, Longitude) into the mark object ( mark.data.Title = listMarkers[i] ), and then read them in event handler from evt or this object (as you're setting it in register call). 要解决此问题,请将详细信息(标题,纬度,经度)添加到标记对象( mark.data.Title = listMarkers[i] )中,然后在事件处理程序中从evtthis对象中读取它们(在其中进行设置) register通话)。

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

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