Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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...

share|improve this answer
    
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 Jul 13 '12 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. –  Aragon Jul 13 '12 at 8:28

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

share|improve this answer
    
Yeah, that's what I did to get it working. Very annoying. Things like this don't result in nice code –  Jeroen Jul 13 '12 at 8:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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