Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: fitBounds without an animation #1473

Closed
Tracked by #229
kristfal opened this issue Sep 10, 2015 · 23 comments
Closed
Tracked by #229

API: fitBounds without an animation #1473

kristfal opened this issue Sep 10, 2015 · 23 comments

Comments

@kristfal
Copy link

Hei!

fitBounds fires off either easeTo or FlyTo. Both are animated. Would be great if we could pass along a param to trigger jumpTo.

@1ec5
Copy link
Contributor

1ec5 commented Sep 10, 2015

jumpTo() is the equivalent of easeTo() with a duration of 0. Try setting duration: 0 in the options object you pass in.

@kristfal
Copy link
Author

@1ec5 Ah, makes sense. Just one thing, fitBounds does a check for a linear option. This isn't documented nor used in any other public methods, just internally. Makes it slightly tricky to follow. Anyway, feel free to close this issue unless you want to keep it open as an doc-improvement issue.

fitBounds: function(options) {

(...)
 return options.linear ?
            this.easeTo(options) :
            this.flyTo(options);

@tmcw
Copy link
Contributor

tmcw commented Oct 12, 2015

I think we should add sync API methods and clear ways to use them: setting a duration of 0 is not a very good way to express this.

@peckjon
Copy link
Contributor

peckjon commented Jan 22, 2016

@1ec5 Request for clarification: the docs on jumpTo indicate there's no transition, but easeTo with duration:0 (or linear: true) still shows a (very fast) animation. Is this the expected behavior? If so, we still do need a way to fitBounds() with no transition.

@bhousel
Copy link
Contributor

bhousel commented Jan 22, 2016

@1ec5 Request for clarification: the docs on jumpTo indicate there's no transition, but easeTo with duration:0 (or linear: true) still shows a (very fast) animation. Is this the expected behavior?

In my opinion, this is wrong - we should special case duration:0 and have it call jumpTo, instead of triggering a very fast animation.

Thoughts, @1ec5 ?

@1ec5
Copy link
Contributor

1ec5 commented Jan 22, 2016

I agree. gl-native runs zero-second transitions atomically and synchronously. Should the change take place in _ease(), to benefit all the camera methods, or would that introduce unexpected behavior? I see there's already an animated option that would become redundant.

@mourner
Copy link
Member

mourner commented Jan 22, 2016

It is in fact special-cased already. So the 0-duration @peckjon describes is definitely a bug.

@peckjon
Copy link
Contributor

peckjon commented Jan 22, 2016

Can someone else verify the bug please? Want to be sure it's not just my codebase.

On Jan 22, 2016, at 07:29, Vladimir Agafonkin notifications@github.com wrote:

It is in fact special-cased already. So the 0-duration @peckjon describes is definitely a bug.


Reply to this email directly or view it on GitHub.

@1ec5
Copy link
Contributor

1ec5 commented Jan 22, 2016

fitBounds() with duration: 0 does run synchronously without animation in GL JS 0.12.4. Are you setting any other options?

@peckjon
Copy link
Contributor

peckjon commented Jan 22, 2016

@mourner @1ec5 thank you for your help! I've been able to confirm it was a bug in my local code, and 0.12.4 works properly with no-animation.

Presumably, then, the best way to create a Map with specific bounds (and no other side effects) is to call fitBounds with {duration: 0} immediately after creation?

cross-ref: #1970

@jfirebaugh
Copy link
Contributor

@peckjon Yes, that's correct.

In terms of API here, I think we should add jumpToBounds, easeToBounds, and flyToBounds and remove fitBounds. Internally the new methods would call a common method to calculate the effective zoom and center for the bounds, then delegate to jumpTo/easeTo/flyTo.

@1ec5
Copy link
Contributor

1ec5 commented Jan 22, 2016

gl-native publicly exposes a cameraForBounds() method that can be passed into *to() or used for any other purpose.

@mourner
Copy link
Member

mourner commented Jan 25, 2016

In terms of API here, I think we should add jumpToBounds, easeToBounds, and flyToBounds and remove fitBounds

I'm 👍 on this, and also adding map.getBoundsZoom or something like this.

@1ec5
Copy link
Contributor

1ec5 commented Jan 25, 2016

Before fitBounds can be subsumed by the *to() methods, the padding camera option needs to be ported from mapbox/mapbox-gl-native#3583.

.getBoundsZoom() is a somewhat confusing name, since it wouldn't get the current bounds or zoom level. How about .toCameraOptions()?

@lucaswoj
Copy link
Contributor

Merged into #2801

@cyrilchapon
Copy link

cyrilchapon commented Sep 19, 2017

I might be stupid, but hey

how to fit bounds without an animation ?

jumpTo with duration 0, or stuff like panTo and panBy are all about a combination of center and zoom, while a bound is like a rectangle.

I know best zoom / center combination can be calculated from a bounds, but the purpose of fitBounds was also to add sugar auto-calculating that, beyond the fact of centering / zooming the map.

I'm aware of #2801, and cameraForBounds or bounds inside a fresh new method are great proposals. I know this is on backlog too, but I'm looking for a kinda workaround solution here

@Erutan409
Copy link

@cyrilchapon Did you find one?

@mollymerp
Copy link
Contributor

mollymerp commented Aug 20, 2018

@cyrilchapon @Erutan409 you can use duration: 0 in the fitBounds options

    map.fitBounds([[
        32.958984,
        -5.353521
    ], [
        43.50585,
        5.615985
    ]], {duration: 0});

Map#fitBounds supports all the options in CameraOptions and AnimationOptions. The documentation for this method is not generating correctly. I'm working on a fix.

@jryio
Copy link

jryio commented Sep 1, 2018

@mollymerp Thank you for this! I didn't connect that duration was available in AnimationOptions on fitBounds. I glossed right the statement in the docs that says

Options supports all properties from AnimationOptions and CameraOptions in addition to the fields below.

@vedmant
Copy link

vedmant commented Jun 23, 2020

This doesn't work for me

this.map.fitBounds(this.bounds, {padding: 30, duration: 0})

Also how I can listed to fitBounds end event?

@cs09g
Copy link
Contributor

cs09g commented Jun 23, 2020

@vedmant could you create working fiddle which is reproducing the issue? For the fitBounds end event, you can try to use moveend event with once.

@vedmant
Copy link

vedmant commented Jun 24, 2020

@cs09g It works actually, it was my mistake. you can try to use moveend event with once. - thanks, I'll try this.

@malihe1991
Copy link

malihe1991 commented Feb 8, 2023

It's easy. You must add animate: false to your fitbounds method options :))

map.fitBounds(bounds, {
     padding: {
         top: 150, bottom: 150, left: 30, right: 30,
     },
     maxZoom: 13,
     animate: false,
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests