简体   繁体   中英

Set two styles on a single point in OpenLayers 3

I'm new to Openlayer. How can I set two styles on a single point? for example an icon and a square together

x.setStyle(new ol.style.Style({
    image: new ol.style.RegularShape({
        fill: new ol.style.Fill({color: 'red'}),
        stroke: new ol.style.Stroke({color: 'black', width: 2}),
        points: 4,
        radius: 10,
        angle: Math.PI / 4
    })
}));
x.setStyle(new ol.style.Style({
    image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        // color: '#8959A8',
        src: '{!! url('/img/sensor_blue.png') !!}',
        scale: 0.3,
        opacity: 0.2
    }))
}));

In this code only the last style is set, but I want two styles together. Thanks for your help.

You can send an array of style object to the ol.Feature#setStyle method. See in the documentation: http://openlayers.org/en/v3.13.1/apidoc/ol.Feature.html#setStyle

x.setStyle([
    new ol.style.Style({
        image: new ol.style.RegularShape({
            fill: new ol.style.Fill({color: 'red'}),
            stroke: new ol.style.Stroke({color: 'black', width: 2}),
            points: 4,
            radius: 10,
            angle: Math.PI / 4
        })
    }),
    new ol.style.Style({
        image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
            // color: '#8959A8',
            src: '{!! url('/img/sensor_blue.png') !!}',
            scale: 0.3,
            opacity: 0.2
        }))
    })
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM