Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openlayers vector feature get pixel position

Tags:

openlayers

What is the fastest way to get the pixel coordinates of a OpenLayers.Feature.Vector? I mean, I want to get the pixel (top,left) position relative to map container of a already drawn feature, if possible without calculations because performance improvements.

Thanks & regards, Rafael.

like image 773
Rafael Avatar asked Jun 13 '14 08:06

Rafael


1 Answers

In ol3:

var geometry = feature.getGeometry();
var coordinate = geometry.getCoordinates();
var pixel = map.getPixelFromCoordinate(coordinate);

In OL2:

var geometry = feature.geometry;
var coordinate = new OpenLayers.LonLat(geometry.x, geometry.y);
var pixel = map.getPixelFromLonLat(coordinate);

A few prerequisities: the feature must be point, otherwise geometry.getCoordinates() returns an array of coordinates and you need to choose one. The other should be pretty obvious but I'll mention it anyway: variable map is an instance of ol.Map or OpenLayers.Map respectivelly

like image 134
Kenny806 Avatar answered Nov 07 '22 12:11

Kenny806