views:

355

answers:

1

The API documentation for OpenLayers.Feature.Vector says that Vector itself has no methods at all.

I know how to let user move the Vector by adding OpenLayers.Control.DragFeature control to map. So if the user can move the Vector then there has to ba a way to move it programmatically too. But I can't figure out how to do it.

+5  A: 

You move an OpenLayers.Feature.Vector object by calling methods on its geometry object, not the vector itself. These methods include move, rotate, resize, and transform.

Note that you will not find any of the methods defined on the OpenLayers.Geometry base object but only on the appropriate child object (note that there are multiple level of inheritance within OpenLayers geometries). OpenLayers.Geometry.Collection is a good example.

You can find a great example of programmatically moving OpenLayers vectors here.

atogle
Alright, I am now able to use `OpenLayers.Geometry.Point.move()` method to move the feature by some amount. But I need to move the feature into a specific location. Is there a simple way to do that or do I have to calculate the amount I have to move it by.
Rene Saarsoo
The move method works by offsetting the x and y properties on the geometry. If you're using a Point (as indicated above) then you could do this:function movePoint(point, x, y) { point.x = x; point.y = y; point.clearBounds();}You can take a look at the source for moving a point here: http://trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Geometry/Point.jsWorking with more complicated geometries will require more work. Check out the move implementation for Collection here: http://trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Geometry/Collection.js
atogle
Thanks, the movePoint() funktion works well.
Rene Saarsoo
BTW, what about this point.clearBounds() method? It doesn't seem to be documented in API. Should I really use it?
Rene Saarsoo
OK. I think I just better remove the feature and add it back to a new location. Don't want to rely on undocumented methods.
Rene Saarsoo
geom.clearBounds() just sets the bounds property to null, but also the bounds property of the parent geometry. The parent property is set when a geometry is added as component of another geometry. See http://trac.openlayers.org/browser/trunk/openlayers/lib/OpenLayers/Geometry.js Being able to read the source code will get you a long way when documentation is scarce. Good luck!
atogle