我根据存储在* .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);
}