我有一系列标记,它们根据存储在 *.js 中的坐标放置在地图上
我能够为每个标记添加一个弹出窗口(如图所示),但这并不是我想要的。
https://dl.dropbox.com/u/108179246/Cattura.JPG
我正在尝试为每个标记插入一个仅在我单击相应标记时才打开的弹出窗口。这是我到目前为止使用的代码。
function showConsideredSamplingPoints() {
var consideredMarkers = new OpenLayers.Layer.Markers( "Considered samplings (" + consideredPoints.length + ")");
var lonLat = null;
for (var i = 0, len = consideredPoints.length; i < len; i++) {
lonLat = new OpenLayers.LonLat(consideredPoints[i].lon, consideredPoints[i].lat)
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
osmmap.getProjectionObject() // to Spherical Mercator Projection
);
var marker= new OpenLayers.Marker(lonLat);
consideredMarkers.addMarker(marker, iconRed.clone());
popup = new OpenLayers.Popup.FramedCloud(i,
lonLat,
new OpenLayers.Size(200, 200),
"example popup"+osmmap.popups.length,
null, true);
popup.autoSize = true;
marker.events.register("click", marker, function(e){
osmmap.popups[i].show();
});
osmmap.addPopup(popup);
}
osmmap.addLayer(consideredMarkers);}
如您所见,我尝试为每个标记插入一个事件,但不起作用。为什么?错误是什么?
解决方案:感谢我的朋友,我找到了解决方案。事件声明中存在错误。正确、完整地携带代码。
var consideredMarkers = new OpenLayers.Layer.Markers("Considered samplings (" + consideredPoints.length + ")")
, marker = []
, lonLat = null
, len
, i
, num
, osmid
, popup;
for (i = 0, len = consideredPoints.length; i < len; i++) {
lonLat = new OpenLayers.LonLat(consideredPoints[i].lon, consideredPoints[i].lat).transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
osmmap.getProjectionObject() // to Spherical Mercator Projection
);
num = consideredPoints[i].num;
osmid = consideredPoints[i].osmid;
marker[i] = new OpenLayers.Marker(lonLat);
consideredMarkers.addMarker(marker[i], iconRed.clone());
popup = new OpenLayers.Popup.FramedCloud(
i, //id
lonLat, // lonlat
new OpenLayers.Size(200, 200), // size
"Point:" + num + " OsmId:" + osmid + "<---", // content
null, // anchor
true // close
);
popup.autoSize = true; // initial setting
popup.hide(); // hide this popup!
osmmap.addPopup(popup); // add to the osmmap popup list
marker[i].events.register("click", popup, function () { // add click event
this.toggle(); // show or hide
});
}
osmmap.addLayer(consideredMarkers);
}