I am very new to openlayers. I have done showing map and whenever we click on the map it is showing marker. But i need to open the popup when i click on the marker.
The following code has some problem. When i click on marker popup is displaying but at the corner it is displaying. I want to display the popup on the marker it self
<html>
<body>
<div id="mapdiv"></div>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>
map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
var lonLat = new OpenLayers.LonLat( -0.1279688 ,51.5077286 )
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var zoom=16;
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
map.events.register("click", map , function(e){
var opx = map.getLonLatFromPixel(e.xy) ;
var marker = new OpenLayers.Marker(opx);
markers.addMarker(marker);
marker.events.register("click", marker, function(e){
popup = new OpenLayers.Popup("chicken",
map.getLonLatFromPixel(e.xy),
new OpenLayers.Size(200,200),
"example popup",
true);
map.addPopup(popup);
});
});
map.setCenter (lonLat, zoom);
</script>
</body>
</html>
Best Answer
Try this: