简体   繁体   中英

Changing Style of the icon using ol3 (Openlayers 3)

I am adding markers and changing its position using the getFeatureById() . Is there a similar way to update the Style and icon to set the feature without creating it again using ?

Currently I am doing it this way:

function addArrowMarker(lat, long, angle, pointerimgsrc, arrowFlag) {

    iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.transform([+long, +lat], 'EPSG:4326', 'EPSG:3857')),
        name: 'NULL'
    });

    iconFeature.setId('arrowMarkerFeature');

    iconStyle = new ol.style.Style({
        image: new ol.style.Icon({
            src: pointerimgsrc,
            rotateWithView: true,
            rotation: angle * Math.PI / 180,
            anchor: [.5, .5],
            anchorXUnits: 'fraction', anchorYUnits: 'fraction',
            opacity: 1
        })
    });

    iconFeature.setStyle(iconStyle);

    vectorArrowSource[arrowFlag] = new ol.source.Vector({
        features: [iconFeature]
    });

    vectorArrowLayer[arrowFlag] = new ol.layer.Vector({
        source: vectorArrowSource[arrowFlag]
    });

    map.addLayer(vectorArrowLayer[arrowFlag]);
}

Now if there is any change in the angle, then I call the function and set a new style again as shown below.

function changeArrowMarker(lat, long, angle, pointerimgsrc,arrowFlag) {    
    var myFeature = vectorArrowSource[arrowFlag].getFeatureById('arrowMarkerFeature');
    myFeature.getGeometry().setCoordinates(ol.proj.transform([+long, +lat], 'EPSG:4326', 'EPSG:3857'));

    iconStyle = new ol.style.Style({
        image: new ol.style.Icon({
            src: pointerimgsrc,
            rotateWithView: true,
            rotation: angle * Math.PI / 180,
            anchor: [.5, .5],
            anchorXUnits: 'fraction', anchorYUnits: 'fraction',
            opacity: 1
        })
    });

    myFeature.setStyle(iconStyle);
}

I there any better approach that . Please help!!

Styles are immutable, so the proper way to "change" them, is to replace them.

However, for your needs, you should consider using a Style Function

function addArrowMarker(lat, long, angle, pointerimgsrc, arrowFlag) {
    iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.transform([+long, +lat], 'EPSG:4326', 'EPSG:3857')),
        name: 'NULL',
        angle: angle
    });

    iconFeature.setId('arrowMarkerFeature');

    iconStyleFunction = function(resolution){
        return [new ol.style.Style({
            image: new ol.style.Icon({
                src: pointerimgsrc,
                rotateWithView: true,
                rotation: this.get('angle') * Math.PI / 180,
                anchor: [.5, .5],
                anchorXUnits: 'fraction', anchorYUnits: 'fraction',
                opacity: 1
            })
        })];
    };

    iconFeature.setStyle(iconStyleFunction);

    vectorArrowSource[arrowFlag] = new ol.source.Vector({
        features: [iconFeature]
    });

    vectorArrowLayer[arrowFlag] = new ol.layer.Vector({
        source: vectorArrowSource[arrowFlag]
    });

    map.addLayer(vectorArrowLayer[arrowFlag]);
}

This way, the style is always calculated based on the "angle" attribute of the feature. In order to move or rotate the geometry/icon, use something like:

function changeArrowMarker(lat, long, angle, arrowFlag) {
    var myFeature = vectorArrowSource[arrowFlag].getFeatureById('arrowMarkerFeature');
    myFeature.getGeometry().setCoordinates(ol.proj.transform([+long, +lat], 'EPSG:4326', 'EPSG:3857'));
    myFeature.set('angle', angle);
}

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