[GIS] How to modify a Marker object’s icon after it’s been drawn

openlayers-2

I'm displaying gps track data (e.g. points in time, like the path a vehicle covered) and I need to allow the user to "play back" the track, by having the markers change color one at a time in sequence. So imagine a sequence of yellow markers and then during playback I change the first one to a red marker, then the next one, etc.

I've done a prototype using point features because it seemed easier at the time to implement this with Points because I could do the following (pseudo code):

p = getpointFromLayer();  // get the yellow point from the layer
p.style = myRedStyle; // change it's style
p.layer.drawFeature(p); // redraw the feature using the red style

Now I'd like to re-do this functionality using Markers because Markers seem to generally be more flexible for other functionality, but I don't know whether it's possible to somehow change a specific marker's appearance. Any ideas?

Best Answer

I have not tried the following code, but I am reasonably sure you should be able to change a marker's icon by simply:

marker.setUrl('http://url.of.your.icon/icon.png');

hide it:

marker.display(false);

enlarge the icon (might pixelize with certain formats):

// 2 = double the size
marker.inflate(2);

Look at source (https://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/Marker.js) for more information.