4

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,
                                        OpenLayers.Handler.RegularPolygon,
                                        {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});
        map.addControl(select);
        select.activate();

3 Answers 3

6

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

Hover

http://dev.openlayers.org/examples/highlight-feature.html

Popup

http://dev.openlayers.org/examples/popupMatrix.html

http://dev.openlayers.org/examples/sundials.html

7
  • I actually just wanted an alt image hover label for my polgonLayer Commented Feb 8, 2012 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. Commented Feb 8, 2012 at 10:41
  • thanks alot, found a way to put text on mouseover in the styleMap Commented Feb 8, 2012 at 11:29
  • 1
    @BlakeLoizides : I'm interested in something similar. Can you tell me how you managed t o put text on mouseover ?
    – Sergiu
    Commented Feb 7, 2013 at 19:15
  • @Sergiu - use the same SelectFeature control, and set hover:true when you create it. Now you have an on-hover handler, not just an on-click handler.
    – Vadim
    Commented Feb 7, 2013 at 21:10
6

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(
          "",
          feature.geometry.getBounds().getCenterLonLat(),
          new OpenLayers.Size(100,100),
          "<div>some text here</div>",
          null,
          true,
          null);

       feature.popup = popup;

       map.addPopup(popup);
       // return false to disable selection and redraw
       // or return true for default behaviour
       return true;
    },
    onUnselect: function(feature) {
       // remove tooltip
       map.removePopup(feature.popup);
       feature.popup.destroy();
       feature.popup=null;
    }
});
0

OpenLayers v.6: How do I handle mouseover for features in OpenLayers 6

(Answers above refer to OL2 (question is tagged with openlayers-2, but title of question is generic) so it may help someone the same answer for version 6)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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