0

Note: This is not same with: How to move OpenLayers Vector programmatically?

I have a simple openlayers map project. I need to show and move some Vectors on of it.

I create vectors like this and that works well:

var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( unit.lon,unit.lat ).transform(epsg4326, projectTo),
                            {description:'This is the value of<br>the description attribute'} ,
                            {externalGraphic: '/assets/admin/layout/img/avatar/' + unit.id + '.png', graphicHeight: 74, graphicWidth: 60, graphicXOffset:-12, graphicYOffset:-25  }
                    );

feature.id = unit.id;
vectorLayer.addFeatures(feature);

However i'm trying to move these vectors to some exact LonLat. I've tried lots of things. One of them is below:

 var feature = vectorLayer.getFeatureById(unit.id);
 movePoint(feature.point, unit.lon, unit.lat);        
vectorLayer.redraw();

function movePoint(point, x, y) { point.x = x; point.y = y; point.clearBounds(); }

Other one is:

var feature = vectorLayer.getFeatureById(unit.id);
feature.geometry.move(unit.lon, unit.lat);
vectorLayer.redraw();

As i understood the last move method uses pixels differences. But i dont want to use difference. Instead of that, use directly exact longitude and latitude parameters.

So again, what is the way to move vectors/points programmatically to an exact location?

I've google maps and also OSM on my project, could the projection issue be a problem?

I just started to develop openlayers. Any help would be greatly appreciated.

Community
  • 1
  • 1
Ismail Yavuz
  • 6,727
  • 6
  • 29
  • 50
  • from your code above, I don't see that you provide the "unit.lon, unit.lat" any new values, have you done that already? – JSC Aug 13 '14 at 16:11
  • Also you use point.clearBounds which means the bounds of the vector will be null, it will display nothing. – JSC Aug 13 '14 at 16:20
  • unit.lon and unit.lat has new values for each time. I've checked it by canary js debugger. @JSC – Ismail Yavuz Aug 14 '14 at 07:30
  • I copied "clear bounds" code from an example and actually i dont know how that works.. @JSC – Ismail Yavuz Aug 14 '14 at 07:32

2 Answers2

2

I believe, the point used to create the vector feature is assigned to the geometry property of that feature.

Try setting feature.geometry.x and feature.geometry.y in your first example instead of setting the feature.point.

Updated with a fiddle, the main part being:

    var targetLoc = new OpenLayers.LonLat(-16, 50).transform(epsg4326, projectTo);
    feature.geometry.x = targetLoc.lon;
    feature.geometry.y = targetLoc.lat;
    vectorLayer.redraw();
gberginc
  • 447
  • 2
  • 4
  • Actually, the signature of the move method, is point.move(x,y) as can be seen here: http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Geometry/Point.js. You could set them directly, but that isn't the problem here. – John Powell Aug 13 '14 at 18:53
  • `move(x, y)` moves the point by the given offset, doesn't it? I have added a simple fiddle to demonstrate the case. – gberginc Aug 13 '14 at 19:27
1

It is very likely to be projection related. If you are using Google Maps and OSM then your coordinate system is 3857 (Spherical Mercator), which is in meters. You will need to convert you lat/lon to this first and then call move, eg,

var fromProj = new OpenLayers.Projection("EPSG:4326");
var toProj = new OpenLayers.Projection("EPSG:3857");
var point = new OpenLayers.LonLat(-1, 52);
point.transform(proj, toProj);
feature.geometry.move(point);

There is some useful information in the docs.

You can also set the mapProjection and displayProjection in the map constructor and then you can use:

point.transform(fromProj, map.getProjectionObject()) as well.

See also this gis.stackexchange.com answer for some more info on settting projection properties of the map.

Community
  • 1
  • 1
John Powell
  • 12,253
  • 6
  • 59
  • 67
  • Thank you for your answer, I am successfully able to move the marker, but as I zoom in or out after moving, the marker disappears, have you faced this issue? – jain Apr 30 '16 at 14:39