I have KML layer with multiple tracks and I would like to highlight them individually on mouse hover. I tried to implement this like in the example found in OpenLayers examples, but couldn't get it work.

In the example there is following code that adds polygon as feature. In my case, how do I add KML-tracks as feature so that I can highlight them with SelectFeature?

var feature = new OpenLayers.Feature.Vector(
        "POLYGON((28.828125 0.3515625, 132.1875 -13.0078125, -1.40625 -59.4140625, 28.828125 0.3515625))"

You can find simplified version of my index.html and KML file here: https://gist.github.com/1379118

1 Answer 1


Your code has no SelectFeature handlers.

Look at the source code of the sample you linked to, there are 2 controls, one that acts on a "highlight" (or a "hover") and the other on a select/click. You need to create handlers like that and add your vector layer to it.

...from the sample file

var highlightCtrl = new OpenLayers.Control.SelectFeature...
var selectCtrl = ...
  • That sample was supposed to only show the starting point where to add highlighting feature. I updated the sample with SelectFeatures included. It doesn't yet work because I should also addFeatures, but don't know how to exactly do it. Commented Nov 20, 2011 at 21:16
  • Vector layer has the function to addFeature. dev.openlayers.org/docs/files/OpenLayers/Layer/…
    – Vadim
    Commented Nov 30, 2011 at 18:30
  • See this example on how the features are created and added to the map. Ignore the rotating part if it's not applicable. openlayers.org/dev/examples/rotate-features.html
    – Vadim
    Commented Nov 30, 2011 at 18:31

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.