使用ol3更改图标样式(Openlayers 3)

时间:2015-08-28 10:39:10

标签: javascript openlayers-3

我正在使用getFeatureById()添加标记并更改其位置。有没有类似的方法来更新样式和图标以设置功能而不使用再次创建它?

目前我这样做:

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]);
}

现在如果角度有任何变化,我会调用该函数并再次设置一个新样式,如下所示。

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);
}

我有更好的方法。请帮忙!!

1 个答案:

答案 0 :(得分:4)

样式是不可变的,所以正确的方式改变"他们是取代他们。

但是,根据您的需要,您应该考虑使用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]);
}

这样,风格总是根据"角度"功能的属性。要移动或旋转几何/图标,请使用以下内容:

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);
}