I have a problem to zoom the nearest possible from extent on a map with Postgis data.
I have try to zoom from geometry but always land in an other location.
In first place I call Postgis with this code
st_xmin(box2d(st_transform(st_buffer(t.geom,5),3857))) xmin,
st_xmax(box2d(st_transform(st_buffer(t.geom,5),3857))) xmax,
st_ymin(box2d(st_transform(st_buffer(t.geom,5),3857))) ymin,
st_xmax(box2d(st_transform(st_buffer(t.geom,5),3857))) ymax
Then I call my api to retrieve this value and do this in my openLayers files
view.setCenter([coord.position_x, coord.position_y])
const zoom = new ZoomToExtent({
extent: [coord.xmin*1, coord.xmax*1, coord.ymin*1, coord.ymax*1]
});
map.addControl(zoom);
view.fit([coord.xmin*1, coord.xmax*1, coord.ymin*1, coord.ymax*1], {
size: map.getSize(),
padding: [50, 50, 50, 50],
nearest: true,
duration: 1000
})
While I got the right center, I stay too far from my element.