OpenLayers 3 - Feature Z-Index on Highlights

I have some problems with z-index on OpenLayers 3 when I want to highlight a function.

I am creating a GeoJSON form of a country, add some marker on top and I want to change the color of the shape when I hover over.

But, when changing colors, the shape will hide my markers.

I'm trying to put a hightlight style zIndex but that doesn't change anything ...

    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })],
        controls: ol.control.defaults({
            attributionOptions: ({
                collapsible: false
            })
        }),
        target: 'map',
        view: new ol.View({
            center: [631965.84, 4918890.2],
            zoom: 3
        })
    });

    var vector = new ol.layer.Vector({
        source: new ol.source.Vector({}),
        style: new ol.style.Style({
            zIndex: 1,
            stroke: new ol.style.Stroke({
                color: '#589CA9',
                width: 3
            }),
            fill: new ol.style.Fill({
                color: '#589CA9'
            })
        })
    });

    map.addLayer(vector);

    var selectPointerMove = new ol.interaction.Select({
        condition: ol.events.condition.pointerMove,
        style: new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: '#EF7F01',
                width: 3

            }),
            zIndex: 1,
            fill: new ol.style.Fill({
                color: '#EF7F01'
            })
        })
    });

    map.addInteraction(selectPointerMove);

    var feature = new ol.format.GeoJSON().readFeature(Some_GeoJSON_Coordinate, {
        dataProjection: ol.proj.get('EPSG:4326'),
        featureProjection: ol.proj.get('EPSG:3857')
    });

    vector.getSource().addFeature(feature);

    iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.transform([5, 44],"EPSG:4326", 'EPSG:3857')),
        type:"marker"
    });

    var iconStyle = new ol.style.Style({
        zIndex:2,
        image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
            anchor:[0.5,1],
            scale:0.1,
            src: 'https://lh4.ggpht.com/Tr5sntMif9qOPrKV_UVl7K8A_V3xQDgA7Sw_qweLUFlg76d_vGFA7q1xIKZ6IcmeGqg=w300'
        }))
    });

    iconFeature.setStyle(iconStyle);

    vector.getSource().addFeature(iconFeature)

      

I am creating a JSFiddle of my problem: http://jsfiddle.net/a1zb5kzf/1/

Thank you in advance for your help.

+3


source to share


1 answer


I found a solution.

According to the Select interaction documentation , it's not just about applying a different style, but moving your function onto a temporary overlay. So zIndex doesn't work because the functions are no longer at the same level.



So, to get my main content and keep my function at the same level, I look at the pointermove event and apply the styling as needed. Shortly before that I memorized this function and applied the default style again

 cartoCtrl.map.on("pointermove", function (evt) {
                    var feature = cartoCtrl.map.forEachFeatureAtPixel(evt.pixel,
                        function (feature) {
                            return feature;
                        });
                    if (feature && feature.getProperties().type != "marker") {
                        cartoCtrl.lastHighlitedFeature = feature;
                        feature.setStyle(highlightStyle)
                        }));
                    } else {
                        if(cartoCtrl.lastHighlitedFeature){
                            cartoCtrl.lastHighlitedFeature.setStyle(defautlStyle);
                            cartoCtrl.lastHighlitedFeature = false;
                        }
                    }
                });

      

+4


source







All Articles