Please try this i hope this will help you. DEMO
This demo will display a point on a map and highlight it on mouse over and selecting point on click will display a popup, and removes the popup on unselect
Here is the code for function :
function init() {
var Geographic = new OpenLayers.Projection("EPSG:4326");
var Mercator = new OpenLayers.Projection("EPSG:900913");
var map = new OpenLayers.Map({
div: "map",
center: new OpenLayers.LonLat(0, 0),
minResolution: "auto",
minExtent: new OpenLayers.Bounds(-1, -1, 1, 1),
maxResolution: "auto",
maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
projection: Mercator,
displayProjection: Geographic
});
var baselayer = new OpenLayers.Layer.OSM();
map.addLayer(baselayer);
map.addControl(new OpenLayers.Control.MousePosition());
map.setCenter(new OpenLayers.LonLat(77.79, 22.77),4);
// creates a point layer with a temporary style
var pointLayer = new OpenLayers.Layer.Vector("Points", {
styleMap: new OpenLayers.StyleMap({
"temporary": new OpenLayers.Style({
strokeColor: '#7f0000',
fillColor: '#7f0000',
strokeOpacity: .8,
strokeWidth: 1,
fillOpacity: .3,
cursor: "default",
pointRadius: 6
})
})
});
// add a point to pointLayer
var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(77.79, 22.77));
pointLayer.addFeatures(feature);
// point layer events on featureselected adds a popup and removes it on featureunselected
pointLayer.events.on({
featureselected: function (e) {
latitude = e.feature.geometry.x;
longitude = e.feature.geometry.y;
id = e.feature.id;
popup = new OpenLayers.Popup(id,
new OpenLayers.LonLat(latitude, longitude),
new OpenLayers.Size(200, 20),
id, true);
map.addPopup(popup);
},
featureunselected: function (e) {
var id = e.feature.id;
for (var i = 0; i < map.popups.length; i++) {
if (map.popups[i].id == id) {
var x = map.popups[i];
map.removePopup(x);
break;
}
}
}
});
map.addLayer(pointLayer);
// highlightControl adds the temporary style on select
var highlightControl = new OpenLayers.Control.SelectFeature(pointLayer, {
hover: true,
highlightOnly: true,
renderIntent: "temporary"
});
// selectControl will select a particular feature
var selectControl = new OpenLayers.Control.SelectFeature([pointLayer],
{ clickout: true }
);
map.addControl(highlightControl);
highlightControl.activate();
map.addControl(selectControl);
selectControl.activate();
}