Reputation: 943
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 openlayers-3?
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!!
Upvotes: 2
Views: 5742
Reputation: 3142
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);
}
Upvotes: 4