2

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?

2 Answers 2

2

you can also do sth like this:

var vectorLayer = new OpenLayers.Layer.Vector("Test");

var feature = new OpenLayers.Feature.Vector(
              new OpenLayers.Geometry.Point(-71, 42),
                  {vcid:'3243223'},
                  {externalGraphic: 'img/marker.png', graphicHeight: 21, graphicWidth: 16});

vectorLayer.addFeatures(feature);
map.addLayer(vectorLayer);

then

vectorLayer.features[2].style.externalGraphic = 'other/path/image.png';
vectorLayer.redraw();

i hope it helps you...

2
  • hi. What's 'sth'? I'll give it a try, but to me it seems a bit weird to use a vector to mimic a marker. Can I still add pop-ups to this element? What does this vcid number do? I can't find documentation on that.
    – Jeroen
    Commented Jul 13, 2012 at 8:19
  • sth is meaning of "like this" :) . vcid is extra information for your point. you also add popup to this. this is my solution actually it is not hard to implement.
    – urcm
    Commented Jul 13, 2012 at 8:28
0

maybe OpenLayers is ignoring your changes, not to recompute everything on each map refresh. I'd try destroying the features and create them again.. regards, JM

1
  • Yeah, that's what I did to get it working. Very annoying. Things like this don't result in nice code
    – Jeroen
    Commented Jul 13, 2012 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.