简体   繁体   中英

openlayers vector feature get pixel position

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM