Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

make a hover/mouseover label for a polygon in Openlayers?

I made a map with draw, delete and drag polygon function.

Now I need to make a hover mouseover for this.

How to get a hover label for polygon

        var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");

        map.addLayers([wmsLayer, polygonLayer]);

        polyOptions = {sides: 40};
        polygonControl = new OpenLayers.Control.DrawFeature(polygonLayer,
                                        {handlerOptions: polyOptions} );

       var myStyles = new OpenLayers.StyleMap({
            "default": new OpenLayers.Style({
                pointRadius: "${type}", // sized according to type attribute
                fillColor: "#ffcc66",
                strokeColor: "#ff9933",
                strokeWidth: 2,
                graphicZIndex: 1
            "select": new OpenLayers.Style({
                fillColor: "#66ccff",
                strokeColor: "#3399ff",
                graphicZIndex: 2

         var select = new OpenLayers.Control.SelectFeature(polygonLayer, {hover: true});
share|improve this question
up vote 5 down vote accepted

Edit: 5/13/2015. Updating links. Thank you @Dennis-van-Schaik



share|improve this answer
I actually just wanted an alt image hover label for my polgonLayer – Blake Loizides Feb 8 '12 at 8:17
@CaptDragon I managed to get a hover effect, But how do I get a label when i hover. A simple label? Your help is always greatly appreciated. Thank you. – Blake Loizides Feb 8 '12 at 10:41
@CaptDragon, Found it thanks. – Blake Loizides Feb 8 '12 at 11:28
thanks alot, found a way to put text on mouseover in the styleMap – Blake Loizides Feb 8 '12 at 11:29
@BlakeLoizides : I'm interested in something similar. Can you tell me how you managed t o put text on mouseover ? – Sergiu Feb 7 '13 at 19:15

The SelectFeature control provides event hooks for this purpose:

var selectControl = new OpenLayers.Control.SelectFeature(
  polygonLayer, {
    hover: true,
    onBeforeSelect: function(feature) {
       // add code to create tooltip/popup
       popup = new OpenLayers.Popup.FramedCloud(
          new OpenLayers.Size(100,100),
          "<div>some text here</div>",

       feature.popup = popup;

       // return false to disable selection and redraw
       // or return true for default behaviour
       return true;
    onUnselect: function(feature) {
       // remove tooltip
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.