mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Fitbounds with center option not doing expected thing

Open malekeym opened this issue 4 years ago • 7 comments

mapbox-gl-js version:2.6.1

browser:Chrome

Steps to Trigger Behavior

1.create LngLatBounds as bounds 2 .use map.fitBounds and pass the center option to it

Link to Demonstration

check out this link for more info: https://jsbin.com/zodotod/edit?html,output

Expected Behavior

The bounds should be in the view and the center of the map should be the center option

Actual Behavior

Just flyTo the center

malekeym avatar Dec 10 '21 08:12 malekeym

I think that center is not an option for the fitBounds method. https://docs.mapbox.com/mapbox-gl-js/api/map/#map#fitbounds

Screen Shot 2021-12-17 at 17 03 12

Al-0 avatar Dec 17 '21 23:12 Al-0

@ai-santos I think, You are wrong image

malekeym avatar Dec 18 '21 15:12 malekeym

Hmmm, now that you mention it, it should be available as part of the CameraOptions, I will look into it.

Al-0 avatar Dec 18 '21 21:12 Al-0

Just to follow up because I have the same problem. I'm using v2.7.0.

Jeremboo avatar Apr 25 '22 17:04 Jeremboo

It's been a while, I can still reproduce it. Any updates?

mattwltr avatar Sep 13 '22 07:09 mattwltr

I got it working with following code. fitBounds( [ Object.values(bounds.southwest).reverse(), Object.values(bounds.northeast).reverse(), ] as LngLatBoundsLike, { center: [longitude, latitude] } );

gauravkhurana17 avatar Dec 14 '22 14:12 gauravkhurana17

Did not work for me. But the following works fine..

let bounds = new Mapbox.LngLatBounds()
addresses.forEach(el => bounds.extend([el.longitude, el.latitude]));

const options = {
    padding: 80,
    duration: 0,
    // center: this.center
}

// calculate center related offset
const ne = bounds.getNorthEast()
const sw = bounds.getSouthWest()

const offset = {
    ne: [
        this.center[0] - ne.lng,
        this.center[1] - ne.lat,
    ],
    sw: [
        this.center[0] - sw.lng,
        this.center[1] - sw.lat,
    ]
}

bounds.extend([this.center[0] + offset.ne[0], this.center[1] + offset.ne[1]])
bounds.extend([this.center[0] + offset.sw[0], this.center[1] + offset.sw[1]])

map.fitBounds(bounds, options)

basically I'm calculating the opposite positions related to the center

artem-schander avatar Mar 01 '24 15:03 artem-schander