9

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.

Rene Saarsoo
  • 13,580
  • 8
  • 57
  • 85

2 Answers2

12

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.

BiAiB
  • 12,932
  • 10
  • 43
  • 63
atogle
  • 11,293
  • 4
  • 21
  • 13
  • 1
    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 Dec 02 '09 at 14:23
  • 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.js Working 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 Dec 02 '09 at 17:16
  • BTW, what about this point.clearBounds() method? It doesn't seem to be documented in API. Should I really use it? – Rene Saarsoo Dec 02 '09 at 18:12
  • 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 Dec 02 '09 at 18:25
  • 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 Dec 02 '09 at 18:48
  • clearBounds() is important because OpenLayers maintains a cache of the edges of a vector feature, so if you were to move a feature to a new location without resetting its bounds, zooming into it may cause it to disappear as vectors are hidden that are known not to be in the new viewport. – Lucent Dec 29 '11 at 16:14
7

This worked for me:

geometry.move(Y_NextPosition - Y_CurrentPosition, X_NextPosition - X_CurrentPosition)
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
danielB
  • 4,262
  • 1
  • 15
  • 4
  • Seems like you could just set the `x` and `y` values of a point directly to avoid that needless computation. – Andrew Mao Jun 19 '13 at 19:51
  • Is your geometry object feature.geometry or sth else? I tried this but not work, why?: var feature = vectorLayer.getFeatureById(id); feature.geometry.move(lon, lat); @danielB – Ismail Yavuz Aug 13 '14 at 08:52