Map breaks when created dynamically in combination with disabled worldCopyJump
See original GitHub issueI’ve come across a strange issue when creating the map container in JS, appending it to a wrapper and setting the map size at a later time. This only occurs when worldCopyJump is disabled, however.
https://jsfiddle.net/2w6apqxy/
The map tiles are partially broken and the map center is at an incorrect position. When appending the container before L.map()
(https://jsfiddle.net/2w6apqxy/1/) or after L.tileLayer()
(https://jsfiddle.net/2w6apqxy/2/), the issue isn’t present. When enabling worldCopyJump, the issue isn’t present either, regardless of when the container is appended to the wrap.
Is this a bug related to how the map dimensions are determined when worldCopyJump is disabled vs. enabled, or is this expected behavior?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
how to change worldCopyJump value dynamically in leaflet?
You can access the map handlers as map properties, then enable and disable them. As the worldCopyJump option is evaluated when enabling the ......
Read more >Documentation - a JavaScript library for interactive maps
Checks if the map container size changed and updates the map if so — call it after you've changed the map size dynamically,...
Read more >Leaflet's worldCopyJump breaks map, zooming fixes it up ...
I had my map working with worldCopyJump set to true , but then it stopped working. I'm not sure exactly what broke it...
Read more >leaflet - UNPKG
Object): Object\r\n// Compatibility polyfill for [Object.create](https:// ... changed the map size dynamically, also animating\r\n\t// pan by default.
Read more >leaflet | Yarn - Package Manager
JavaScript library for mobile-friendly interactive maps. gis, map. readme. Leaflet was created 11 years ago by Volodymyr Agafonkin, a Ukrainian citizen ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @alex2wong,
Your screenshot and description look like a totally different issue from what OP described.
I suspect your case to be similar to: https://stackoverflow.com/questions/38835758/leaflet-drawing-tiles-disjointly/38836996#38836996
Hi,
Hum looks like a funny edge case indeed.
What happens is that the
map._sizeChanged
remains attrue
fromL.map
initialization.Since
center
andzoom
options are defined inL.map
, the map callssetView
method, which will usemap.getSize
(in_resetView
). But a little bit later the map initialization (re)sets the flagmap._sizeChanged = true
.https://github.com/Leaflet/Leaflet/blob/ba6f97fff8647e724e4dfe66d2ed7da11f908989/src/map/Map.js#L145-L154
Therefore on next
map.getSize()
call, it will re-read the map viewport size.https://github.com/Leaflet/Leaflet/blob/ba6f97fff8647e724e4dfe66d2ed7da11f908989/src/map/Map.js#L852-L853
This interferes with how
invalidateSize
works, which needs to read the old size first. But since the_sizeChanged
flag is stilltrue
, the first (“old”) reading already uses the new size. This makes the view reset fail.https://github.com/Leaflet/Leaflet/blob/ba6f97fff8647e724e4dfe66d2ed7da11f908989/src/map/Map.js#L533-L540
This situation occurs when the map view is set at initialization, then viewport size changes before next call to
map.getSize()
(in OP’s case when appending the map container => width already changes, then adding a Tile Layer, explicitly callingmap.invalidateSize()
, etc.).It does not occur if specifying the option
worldCopyJump
, because theMap.Drag
handler adds an init hook that is called after themap._sizeChanged = true
flag, and if the option istrue
, it callsmap.getSize()
already, therefore before the map viewport size changes.Therefore the fix seems to be to perform the private variables initialization before setting the map view: https://jsfiddle.net/2w6apqxy/3/