OpenLayers 弹出窗口未切换

发布于 2024-12-08 03:07:15 字数 2131 浏览 1 评论 0原文

当我创建静态标记并附加静态弹出窗口时,一切似乎工作正常。我正在尝试构建动态标记,虽然标记和弹出窗口已正确创建,但它们不再因事件而切换。

我已将代码精简到足以显示地图并绘制一个构造的标记/弹出窗口二人组。也许你能看到我缺少的东西?

<html>
<head>
    <style type="text/css">html,body,#basicMap{width:69%;height:60%;}</style>
    <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
    <script>

        /**/var oldProjection=new OpenLayers.Projection("EPSG:4326");//Used to scale the GPS coordinates
        /**/var newProjection=new OpenLayers.Projection("EPSG:900913");//To the map
        /**/var marks=new OpenLayers.Layer.Markers("Debug Site");
        /**/var mapnik=new OpenLayers.Layer.OSM("World Map");//Layer containing the map tiles
        /**/var size=new OpenLayers.Size(21,25);//Icon height and width
        /**/var offset=new OpenLayers.Pixel(-(size.w/2),-size.h);//Icon offset
        /**/var mapCenter=new OpenLayers.LonLat(-125.2,54.8).transform(oldProjection,newProjection);

        function init() 
        {        
            var map=new OpenLayers.Map($("BasicMap"),{controls:[]});
            map.addLayer(mapnik);
            map.setCenter(mapCenter,3);
            map.addLayer(marks);
            var TestSite=new site(-131,57.2,"TestSite","DE","Hello World");
            map.addPopup(TestSite.popup);
        }
        function site(lon,lat,siteID,layer,content)
        {
            this.content=content;
            this.layer=layer;
            this.lon=lon;this.lat=lat;this.siteID=siteID;
            this.lonlat=new OpenLayers.LonLat(lon,lat).transform(oldProjection,newProjection);
            this.popup=new OpenLayers.Popup.Anchored(this.siteID,this.lonlat,new OpenLayers.Size(150,375),this.content);
            this.marker=new OpenLayers.Marker(this.lonlat);
            this.popup.border='1px solid black';this.popup.autoSize=true;
            this.marker.events.register("click",this.marker,function(e){this.popup.toggle()});
            marks.addMarker(this.marker);   
        }
    </script>
</head>
<body onload="init();"><div id="basicMap"></div></body>

When I create a static marker and attach a static popup, things seem to work fine. I am trying to build dynamic markers, and while the marker's and popup's are being created correctly, they are no longer toggling due to events.

I've stripped the code down to just enough to bring the map up and draw one constructed marker/popup duo. Maybe you can see something I'm missing?

<html>
<head>
    <style type="text/css">html,body,#basicMap{width:69%;height:60%;}</style>
    <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
    <script>

        /**/var oldProjection=new OpenLayers.Projection("EPSG:4326");//Used to scale the GPS coordinates
        /**/var newProjection=new OpenLayers.Projection("EPSG:900913");//To the map
        /**/var marks=new OpenLayers.Layer.Markers("Debug Site");
        /**/var mapnik=new OpenLayers.Layer.OSM("World Map");//Layer containing the map tiles
        /**/var size=new OpenLayers.Size(21,25);//Icon height and width
        /**/var offset=new OpenLayers.Pixel(-(size.w/2),-size.h);//Icon offset
        /**/var mapCenter=new OpenLayers.LonLat(-125.2,54.8).transform(oldProjection,newProjection);

        function init() 
        {        
            var map=new OpenLayers.Map($("BasicMap"),{controls:[]});
            map.addLayer(mapnik);
            map.setCenter(mapCenter,3);
            map.addLayer(marks);
            var TestSite=new site(-131,57.2,"TestSite","DE","Hello World");
            map.addPopup(TestSite.popup);
        }
        function site(lon,lat,siteID,layer,content)
        {
            this.content=content;
            this.layer=layer;
            this.lon=lon;this.lat=lat;this.siteID=siteID;
            this.lonlat=new OpenLayers.LonLat(lon,lat).transform(oldProjection,newProjection);
            this.popup=new OpenLayers.Popup.Anchored(this.siteID,this.lonlat,new OpenLayers.Size(150,375),this.content);
            this.marker=new OpenLayers.Marker(this.lonlat);
            this.popup.border='1px solid black';this.popup.autoSize=true;
            this.marker.events.register("click",this.marker,function(e){this.popup.toggle()});
            marks.addMarker(this.marker);   
        }
    </script>
</head>
<body onload="init();"><div id="basicMap"></div></body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文