{"action":"created","issue":{"url":"https://api.github.com/repos/mapbox/mapbox-gl-js/issues/1690","repository_url":"https://api.github.com/repos/mapbox/mapbox-gl-js","labels_url":"https://api.github.com/repos/mapbox/mapbox-gl-js/issues/1690/labels{/name}","comments_url":"https://api.github.com/repos/mapbox/mapbox-gl-js/issues/1690/comments","events_url":"https://api.github.com/repos/mapbox/mapbox-gl-js/issues/1690/events","html_url":"https://github.com/mapbox/mapbox-gl-js/issues/1690","id":116145828,"node_id":"MDU6SXNzdWUxMTYxNDU4Mjg=","number":1690,"title":"setCenter / setZoom don't work the first time if map initialized without a center/zoom","user":{"login":"samanpwbb","id":108094,"node_id":"MDQ6VXNlcjEwODA5NA==","avatar_url":"https://avatars0.githubusercontent.com/u/108094?v=4","gravatar_id":"","url":"https://api.github.com/users/samanpwbb","html_url":"https://github.com/samanpwbb","followers_url":"https://api.github.com/users/samanpwbb/followers","following_url":"https://api.github.com/users/samanpwbb/following{/other_user}","gists_url":"https://api.github.com/users/samanpwbb/gists{/gist_id}","starred_url":"https://api.github.com/users/samanpwbb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samanpwbb/subscriptions","organizations_url":"https://api.github.com/users/samanpwbb/orgs","repos_url":"https://api.github.com/users/samanpwbb/repos","events_url":"https://api.github.com/users/samanpwbb/events{/privacy}","received_events_url":"https://api.github.com/users/samanpwbb/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":{"login":"samanpwbb","id":108094,"node_id":"MDQ6VXNlcjEwODA5NA==","avatar_url":"https://avatars0.githubusercontent.com/u/108094?v=4","gravatar_id":"","url":"https://api.github.com/users/samanpwbb","html_url":"https://github.com/samanpwbb","followers_url":"https://api.github.com/users/samanpwbb/followers","following_url":"https://api.github.com/users/samanpwbb/following{/other_user}","gists_url":"https://api.github.com/users/samanpwbb/gists{/gist_id}","starred_url":"https://api.github.com/users/samanpwbb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samanpwbb/subscriptions","organizations_url":"https://api.github.com/users/samanpwbb/orgs","repos_url":"https://api.github.com/users/samanpwbb/repos","events_url":"https://api.github.com/users/samanpwbb/events{/privacy}","received_events_url":"https://api.github.com/users/samanpwbb/received_events","type":"User","site_admin":false},"assignees":[{"login":"samanpwbb","id":108094,"node_id":"MDQ6VXNlcjEwODA5NA==","avatar_url":"https://avatars0.githubusercontent.com/u/108094?v=4","gravatar_id":"","url":"https://api.github.com/users/samanpwbb","html_url":"https://github.com/samanpwbb","followers_url":"https://api.github.com/users/samanpwbb/followers","following_url":"https://api.github.com/users/samanpwbb/following{/other_user}","gists_url":"https://api.github.com/users/samanpwbb/gists{/gist_id}","starred_url":"https://api.github.com/users/samanpwbb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samanpwbb/subscriptions","organizations_url":"https://api.github.com/users/samanpwbb/orgs","repos_url":"https://api.github.com/users/samanpwbb/repos","events_url":"https://api.github.com/users/samanpwbb/events{/privacy}","received_events_url":"https://api.github.com/users/samanpwbb/received_events","type":"User","site_admin":false}],"milestone":null,"comments":6,"created_at":"2015-11-10T16:35:47Z","updated_at":"2018-08-29T16:44:23Z","closed_at":"2016-05-02T17:50:13Z","author_association":"MEMBER","body":"I was building a little ui for switching between styles and ran into a problem. I wanted a function to apply every time the user paged to a new style, and I wanted the same code to work for the very first style and all subseqent style changes. Function looked sort of like this:\n\n``` js\nfunction setStyle() {\n    mapboxgl.accessToken = next.token;\n    map.setStyle(next.style);\n    map.setCenter(next.center);\n    map.setZoom(next.zoom - .5);\n    map.flyTo({\n        speed: .05,\n        zoom: next.zoom\n    });\n};\n```\n\nAnd I was executing it like this, in an event handler:\n\n``` js\nif (!map) {\n    map = new mapboxgl.Map({\n        container: 'js-map'\n    });\n    map.addControl(new mapboxgl.Navigation({ position: 'top-left' }));\n    setStyle();\n} else {\n    setStyle();\n}\n```\n\nWith this code, the map center would end up at: `{ lng: -117.84767918833975, lat: -1.1368683772161603e-13}` and zoom would be `12.134854352584124` when the code first ran. I ended up just adding a center and zoom property when initializing the map to get around the problem, but should that be necessary? Seems like a bug, docs don't say anywhere that zoom and center are required options.\n\nI tried putting the setStyle() inside a map.on('load', ...) in the first part of the if statement, but that didn't have any effect.\n"},"comment":{"url":"https://api.github.com/repos/mapbox/mapbox-gl-js/issues/comments/417022268","html_url":"https://github.com/mapbox/mapbox-gl-js/issues/1690#issuecomment-417022268","issue_url":"https://api.github.com/repos/mapbox/mapbox-gl-js/issues/1690","id":417022268,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNzAyMjI2OA==","user":{"login":"karantza","id":2000079,"node_id":"MDQ6VXNlcjIwMDAwNzk=","avatar_url":"https://avatars3.githubusercontent.com/u/2000079?v=4","gravatar_id":"","url":"https://api.github.com/users/karantza","html_url":"https://github.com/karantza","followers_url":"https://api.github.com/users/karantza/followers","following_url":"https://api.github.com/users/karantza/following{/other_user}","gists_url":"https://api.github.com/users/karantza/gists{/gist_id}","starred_url":"https://api.github.com/users/karantza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/karantza/subscriptions","organizations_url":"https://api.github.com/users/karantza/orgs","repos_url":"https://api.github.com/users/karantza/repos","events_url":"https://api.github.com/users/karantza/events{/privacy}","received_events_url":"https://api.github.com/users/karantza/received_events","type":"User","site_admin":false},"created_at":"2018-08-29T16:44:23Z","updated_at":"2018-08-29T16:44:23Z","author_association":"NONE","body":"I just encountered this bug too when attempting to initialize my viewport, and calling setCenter after setZoom rather than before fixed it. (I was actually using the `diffStyles` function to generate these commands, and it generated them in the bad order!)\r\n\r\nI dug into it a little bit to see if I could find the bug. I'm fairly sure it has to do with the call to [_constrain](https://github.com/mapbox/mapbox-gl-js/blob/855b850e62196c46f0987ab73600fffddc3e22bc/src/geo/transform.js#L498) when setting `center` that limits its range. Since we haven't set a zoom properly yet, I think some of the scaling going on in there is incorrect, limiting the latitude down to something very small. Didn't have the time to figure out how exactly but hopefully this can point someone in the right direction!"}}
{"actor":{"display_login":"karantza"}}