0

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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • The extent for `fit` should be in the order `[xmin, ymin, xmax, ymax]` – Mike May 02 '23 at 14:52
  • I have change the position but now, i have this error : not fit empty extent provided as `geometry`. But in an other functionality, this code works. I search this for a polygon. – florian paumier May 02 '23 at 17:55
  • 1
    Should `st_xmax(box2d(st_transform(st_buffer(t.geom,5),3857))) ymax` be `st_ymax(box2d(st_transform(st_buffer(t.geom,5),3857))) ymax` ? – Mike May 02 '23 at 18:09

0 Answers0