Openlayers - Redraw() layer. Add / Remove layer.

Posted by Ozaki on Stack Overflow See other posts from Stack Overflow or by Ozaki
Published on 2010-05-18T08:22:04Z Indexed on 2010/05/20 5:00 UTC
Read the original article Hit count: 2106

Filed under:
|
|
|
|

TLDR: I have an Openlayers map with a layer called 'track' I want to remove track and add track back in.


I have an image 'imageFeature' on a layer that rotates on load to the direction being set. I want it to update this rotation that is set in 'styleMap' on a layer called 'tracking'.

  1. I set the var 'stylemap' to apply the external image & rotation.
  2. The 'imageFeature' is added to the layer at the coords specified.
  3. 'imageFeature' is removed.
  4. 'imageFeature' is added again in its new location. Rotation is not applied..

As the 'styleMap' applies to the layer I think that I have to remove the layer and add it again rather than just the 'imageFeature'

Layer:

     var tracking = new OpenLayers.Layer.GML("Tracking", "coordinates.json",
          { format: OpenLayers.Format.GeoJSON,
            styleMap: styleMap

     });

styleMap:

var styleMap = new OpenLayers.StyleMap({
    fillOpacity: 1,
    pointRadius: 10,
    rotation: heading,
});

Now wrapped in a timed function the imageFeature:

map.layers[3].addFeatures(new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(longitude, latitude), {rotation: heading, type: parseInt(Math.random() * 3)}
        ));

Type refers to a lookup of 1 of 3 images.:

           styleMap.addUniqueValueRules("default", "type", lookup);

           var lookup = {
            0: {externalGraphic: "Image1.png", rotation: heading},
            1: {externalGraphic: "Image2.png", rotation: heading},
            2: {externalGraphic: "Image3.png", rotation: heading}
            }

I have tried the 'redraw()' function: but it returns "tracking is undefined" or "map.layers[2]" is undefined.

tracking.redraw(true);
map.layers[2].redraw(true);

Heading is a variable: from a JSON feed.

var heading = 13.542;

But so far can't get anything to work it will only rotate the image onload. The image will move in coordinates as it should though.

So what am I doing wrong with the redraw function or how can I get this image to rotate live?

Thanks in advance

-Ozaki

Add: I managed to get

 map.layers[2].redraw(true);

to sucessfully redraw layer 2. But it still does not update the rotation. I am thinking because the stylemap is updating. But it runs through the style map every n sec, but no updates to rotation and the variable for heading is updating correctly if i put a watch on it in firebug.

© Stack Overflow or respective owner

Related posts about openlayers

Related posts about JavaScript