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 would like to programatically change the geometry of a polygon. feature.geometry.move() works ok, but i rather need supply a whole new geometry. When i try feature.geometry = newGeometry, the feature duplicates itself, rather then changes its position. I read the relevant code in OpenLayers and i cant see why is this happening. Any help welcomed.

function init(){
    var map = new OpenLayers.Map('map');
    map.addLayer(new OpenLayers.Layer.OSM("Mapnik"));
    map.setCenter(fromLL(new OpenLayers.LonLat(14.302,50.093561)), 14);    


    //vector layer
    var layer = new OpenLayers.Layer.Vector("");
    map.addLayer(layer);

    //one feature
    var geometry = fromLL(OpenLayers.Bounds.fromString("14.2989,50.0935,14.3018,50.0965")).toGeometry();
    var rectangle = new OpenLayers.Feature.Vector(geometry);
    layer.addFeatures(rectangle);

    //move it 30pixels up - works
    rectangle.geometry.move(0,map.getResolution()*30);
    layer.redraw();

    //change geometry - wrong - duplicates
    rectangle.geometry = fromLL(OpenLayers.Bounds.fromString("14.302,50.0935,14.6018,50.0965")).toGeometry();
    rectangle.geometry.clearBounds();
    layer.redraw();
}

function toLL(obj){return obj.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));}
function fromLL(obj){return obj.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));}
share|improve this question
    
Soemthing to try first is to set the name of your vector layer - var layer = new OpenLayers.Layer.Vector("MyLayer"); –  geographika Jul 19 '11 at 16:56
    
Also try layer.drawFeature(rectangle); after you've changed the geometry –  geographika Jul 19 '11 at 17:07
    
drawFeature(rectangle) and redraw() acts the same, nameing the layer also didnt help. But thanks for ideas –  zbycz Jul 21 '11 at 14:59

1 Answer 1

Destroying the features then adding the new ones to the layers should work:

...
    //move it 30pixels up - works
    rectangle.geometry.move(0, map.getResolution() * 30);
    layer.redraw();

    layer.destroyFeatures(rectangle);

    rectangle = new OpenLayers.Feature.Vector(fromLL(OpenLayers.Bounds.fromString("14.302,50.0935,14.6018,50.0965")).toGeometry());
    layer.addFeatures(rectangle);
...

EDIT #1

If you want to move the feature with the mouse you can use OpenLayers.Control.DragFeature.

Here's an example

share|improve this answer
    
Yes, thats works as expected. But ive considered it a bit untidy. Since i want to move this rectangle realtime by mouse, creating a new object on every mousemove doesnt seem right. –  zbycz Jul 21 '11 at 15:10
1  
Ahh, then use OpenLayers.Control.DragFeature.... see my edit. –  CaptDragon Jul 21 '11 at 17:07
    
Thanks! that's what I needed. But as zbycz says, it feels untidy, but I have now choice with the way I do (drawing and animating a part of a pathway) –  BiAiB Dec 28 '12 at 15:48

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.