I'm having much trouble changing my (feature)marker icons 'on the fly'. The problem is that I use much different images and also need to change the offset.
So I iterate over every marker and make a new icon object with the latest data:
if (iconImgUrl.indexOf('img/icons/icon_') != -1) {
var size = config.numberPlateTail;
var offset = new OpenLayers.Pixel(0, -size.h);
var iconImg = new OpenLayers.Icon('/img/icons/marker_' + dataitem.numberplate + '.png', size, offset);
} else {
var size = config.iconSize;
var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
var iconImg = new OpenLayers.Icon('/' + iconImgUrl, size, offset);
}
Because I also want to change the offset, I can't use
icon.setUrl();
icon.setSize();
I tried to 'kill' the current icon object and change it for a new one and then try to force it to redraw the icon, but somehow the marker doesn't display after that.
In the update loop I have:
tbPosition.features[i].marker.icon.destroy();
tbPosition.features[i].marker.icon = iconImg;
tbPosition.features[i].marker.draw();
tbPosition.features[i].marker.icon.display(true);
and markers are created like:
var feature = new OpenLayers.Feature(this.markers, new OpenLayers.LonLat(lat, lng));
feature.data.icon = iconImg;
var marker = feature.createMarker();
this.markers.addMarker(marker);
this.features.push(feature);
So, how do I change (or update) the complete icon object?