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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With