Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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("");

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

    //move it 30pixels up - works

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

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

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);


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


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


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.