当我创建静态标记并附加静态弹出窗口时,事情看起来很好。我正在尝试构建动态标记,并且在正确创建标记和弹出窗口时,它们不再由于事件而切换。
我已经将代码剥离到足以使地图上升并绘制一个构造的标记/弹出二重奏。也许你能看到我失踪的东西?
<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>