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

moveend setCenter stack overflow #4398

Closed
stackTom opened this issue Mar 10, 2017 · 3 comments
Closed

moveend setCenter stack overflow #4398

stackTom opened this issue Mar 10, 2017 · 3 comments

Comments

@stackTom
Copy link

mapbox-gl-js version:

Steps to Trigger Behavior

  1. set the map center on moveend with map.setCenter(map.getCenter().wrap());

Expected Behavior

map sets the new center and doesn't crash

Actual Behavior

exception indicating stack overflow is thrown

I am trying to follow this suggestion: #2071 (comment)
By using this code:
map.on('moveend', function() { map.setCenter(map.getCenter().wrap()); });

But, when doing that, an exception is thrown once I pan the map:
Uncaught RangeError: Maximum call stack size exceeded at e.Evented.fire (evented.js:81) at e.i.jumpTo (camera.js:445) at e.i.setCenter (camera.js:80) at e.<anonymous> (mainMap.js:858) at e.Evented.fire (evented.js:87) at e.i.jumpTo (camera.js:461) at e.i.setCenter (camera.js:80) at e.<anonymous> (mainMap.js:858) at e.Evented.fire (evented.js:87) at e.i.jumpTo (camera.js:461) at e.i.setCenter (camera.js:80) at e.<anonymous> (mainMap.js:858)

@stevage
Copy link
Contributor

stevage commented Mar 10, 2017

I think you have two problems here:

  1. You're using map.on instead of map.once, so the handler keeps getting called for every subsequent move.
  2. You're creating a move event from within a move handler, causing a never-ending loop.

Combine those two things, and you have a never-ending snowball, which quickly explodes.

You can kill the loop by passing a property as event data:

map.once('moveend', function(e) { 
  if (e.source === 'recenter')
    return;
  map.setCenter(map.getCenter().wrap(), { source: 'recenter' }); 
});

@igal1c0de4n
Copy link

solved it for me. Thanks!

@maximelafarie
Copy link

Doesn't work anymore in latest mapbox gl version

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

No branches or pull requests

5 participants