Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to make the features from a kml layer highlight on hover, but it doesn't seem to work for me. On the other hand, if i create a vector layer with a polygon feature it works just fine. I made an example with kml and polygon feature just to try to figure out what the difference is. Does anyone know what I am missing? This is the .js code:

var map;

function init(){
        map = new OpenLayers.Map('map');

        var kml = new OpenLayers.Layer.Vector ('Australia Boundaries',{isBaseLayer:true, protocol: new OpenLayers.Protocol.HTTP ({url: 'kml/australia.kml', format: new OpenLayers.Format.KML({ extractStyles: true, extractAttributes: true})}),projection: new OpenLayers.Projection("EPSG:4326"), strategies: [new OpenLayers.Strategy.Fixed()]});
        map.addLayers([kml]);

    //create vector layer with a polygon feature

        var vector = new OpenLayers.Layer.Vector('Vector', {projection: new OpenLayers.Projection("EPSG:4326") });

        var point = new OpenLayers.Geometry.Point(90.752725,10.475584);
        var pointList = [];
        for(var p=0; p<6; ++p) {
            var a = p * (2 * Math.PI) / 7;
            var r = Math.random(1) + 20;
            var newPoint = new OpenLayers.Geometry.Point(point.x + (r * Math.cos(a)),
                                                         point.y + (r * Math.sin(a)));
            pointList.push(newPoint);
        }
        pointList.push(pointList[0]);

        var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
        var polygonFeature = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Polygon([linearRing]));
        vector.addFeatures([polygonFeature]);
        map.addLayer(vector);

   //create highlight and select controls for the vector and kml layers

        var highlightCtrlVector = new OpenLayers.Control.SelectFeature(vector, {
            hover: true,
            highlightOnly: true, 
            renderIntent: 'temporary'});

        var selectCtrlVector = new OpenLayers.Control.SelectFeature(vector,
            {clickout: true}
        );
        var highlightCtrlKml = new OpenLayers.Control.SelectFeature(kml, {
            hover: true,
            highlightOnly: true,
            renderIntent:'temporary'});

        var selectCtrlKml = new OpenLayers.Control.SelectFeature(kml,
            {clickout: true}
        );
        map.addControl(highlightCtrlVector);
        map.addControl(selectCtrlVector);
        map.addControl(highlightCtrlKml);
        map.addControl(selectCtrlKml);
        highlightCtrlVector.activate();
        highlightCtrlKml.activate();
        selectCtrlVector.activate();
        selectCtrlKml.activate();

        map.addControl(new OpenLayers.Control.LayerSwitcher());
        map.zoomToMaxExtent();
    }

And here is the example: http://geoteste.comli.com/app/indexTest.html

share|improve this question
    
try to change isBaseLayer:true to false. –  Aragon Dec 12 '12 at 13:04
add comment

2 Answers

Unfortunately, you can have only one SelectFeature control on your map (or one normal and one for highlighting). For testing purposes, don't activate highlightCtrlVector and selectCtrlVector - can you select from KML layer now?

You can add multiple layers to same select control:

var highlightCtrl = new OpenLayers.Control.SelectFeature([vector, kml], { ...
var selectCtrl = new OpenLayers.Control.SelectFeature([vector, kml], { ...

If you are using callbacks on select events, examine selected feature and you can distinguish, in what layer it is. For example:

selectControl.events.on({
    featurehighlighted: function(evt) {
        if (evt.feature.layer.id == yourKMLlayerID) 
            ...
    }
})    
share|improve this answer
add comment

Please try setting "extractStyles: false, e.g.

var kml = new OpenLayers.Layer.Vector ('Australia Boundaries',{isBaseLayer:true, protocol: new OpenLayers.Protocol.HTTP ({url: 'kml/australia.kml', format: new OpenLayers.Format.KML({ extractStyles: false, extractAttributes: true})}),projection: new OpenLayers.Projection("EPSG:4326"), strategies: [new OpenLayers.Strategy.Fixed()]});

It helped me

Thanks

Maxim

share|improve this answer
add comment

Your Answer

 
discard

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.