[OpenLayers-Users] Changing icons in GeoRSS layer?

Gregor Mosheh gregor at hostgis.com
Sat Aug 4 02:48:39 EDT 2007


Holmes, Charles V. wrote:
> Trying to add the marker with the addMarker
> method will probably solve your problem.  

That's fine if I want to add a static marker. My goal here was to 
replace all of the feature icons in-place. I did come up with a method 
to replace all the RSS icons in-place, though. I copied and tailored 
some code from Layer/GeoRSS.js, to create this:

var overlays = new OpenLayers.Layer.GeoRSS(...);
function setOverlayIcons() {
    for (i=0; i<overlays.features.length; i++) {
       var feature = overlays.features[i];
       var size   = new OpenLayers.Size(10,10);
       var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
       var icon   = new 
OpenLayers.Icon('theme/img/move_feature_on.png',size,offset);

       var newdata = feature.data;
       newdata.icon = icon;
       var newfeature = new OpenLayers.Feature(overlays, feature.lonlat, 
newdata);
       var marker = newfeature.createMarker();
       marker.events.register('click', newfeature, overlays.markerClick);
       overlays.features[i] = newfeature;
       overlays.addMarker(marker);
       overlays.removeMarker(feature.marker);
    }
}

Pretty straightforward: "clone" the Feature's data but swap in a new 
Icon; create a new Feature and replace it in the layer; create a new 
marker from the new Feature and place it; delete the old marker. Works 
beautifully!

One can imagine all sorts of uses by slipping a little bit of 
criteria-examining code around the creation of the Icon.


In our particular case for this project, the need is for the marker to 
be determined dynamically, based on RSS content (a <icon> tag for each 
item). So I won't be using this trick after all; I'll have to modify the 
source code to set the icon during the RSS parsing.

Ah well; a learning experience of this caliber is never a waste of time. :)


Kudos to the OL team on the code, BTW. It's very comprehensible, easy to 
thumb through and see what does what.

-- 
Gregor Mosheh / Greg Allensworth
System Administrator, HostGIS cartographic development & hosting services
http://www.HostGIS.com/

"Remember that no one cares if you can back up,
  only if you can restore." - AMANDA



More information about the Users mailing list