openlayers: getCoordinateFromPixel returns different positions with same pixel

I have a feature representing a vehicle which does not move at all on a map. I want, periodically, to retrieve the point’s coordinates located at a constant distance of the vehicle. When the vehicle does not move, the point’s coordinates should be the same.

I use getCoordinatesFromPixel in order to manage to do that, but, although the pixel passed in argument is still the same, the coordinates I get are differents, resulting in some oscillations on screen.

*How to reproduce : * Assuming you’ve got vehMapPos, an array containing the vehicle position (in map units) and map, the Map. On the Map, rotated with any angle, put a feature and center the map on the feature. Then, launch a setInterval. In this interval, do the following :

var elem = document.getElementById('yourMapId');
var clientRect = elem.getBoundingClientRect();
//Getting pixel coordinates
var centerPixelX = clientRect.width/2;
var tierPixelY = clientRect.height/3;
var pixel = [centerPixelX, 2*tierPixelY];


//Problem is here
var point = map.getCoordinateFromPixel(pixel);

//Creating vector from vehicle to pixel
var X = point[0];
var Y = point[1];

var x = vehMapPos[0];
var y = vehMapPos[1];

var vector = {
   'vx': (x-X),
   'vy': (y-Y)
};

//Centering map to the vector coordinates
var mapCenter  = map.getView().getCenter();
var newCenter = [mapCenter[0] + vector.vx, mapCenter[1] - vector.vy];
map.getView.setCenter(newCenter);

I hope this code helps and do no hesitate to question me if you cannot reproduce the bug. It is very annoying, as we’d like to do a GPS functionality.

Regards

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

OK, so it’s a miscommunication between us, sorry for that.

Obviously. What @tsauerwein and I were trying to explain is that what you are seeing (the “oscillation”) is expected behaviour. A pixel on the map can represent several hundred meters, depending on the resolution. This is the cause for your oscillation. If you did the whole calculation in pixels instead, you’d always round to the center of a screen pixel, and could avoid the oscillation. I’ve updated your fiddle to show how this is done: http://jsfiddle.net/a020po05/5/.

A hint for the future: before accusing a library you’re using of having a bug, it’s better to show a code snippet and ask whether it’s something in your application, or if there could be the possibility of a bug in the library. Sounds much friendlier, doesn’t it?