Skip to content

globe: cameraForBounds returns inconsistent zoom levels #5439

Open
@gsimko

Description

@gsimko

The zoom output of cameraForBounds is inconsistent when using globe projection.
IIUC the culprit is in src/ui/map.ts:622, where cameraHelper is initialized to MercatorCameraHelper(), and only later switched to VerticalPerspectiveCameraHelper.

This also affects the map initialization: when using the bounds parameter, it will not calculate the right zoom level.

The following code reproduces the problem (observe that the console will show two different zoom levels: 1.979 and 1.840, and after 1s waiting the globe will jump to the right level):

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Display a globe with a vector map</title>
    <meta property="og:description" content="Display a globe with a vector map." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='https://unpkg.com/[email protected]/dist/maplibre-gl.css' />
    <script src='https://unpkg.com/[email protected]/dist/maplibre-gl.js'></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; background-color:red; }
    </style>
</head>
<body>
hello
<div id="map"></div>
<script>
    const map = new maplibregl.Map({
        container: 'map',
        style: 'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
        bounds: [-100,30,40,50]
    });

		let cam = map.cameraForBounds([-100,30,40,50])
    console.log(cam.zoom);

    map.on('style.load', () => {
        map.setProjection({
            type: 'globe', // Set projection to globe
        });
  		let cam = map.cameraForBounds([-100,30,40,50])
	    console.log(cam.zoom);
      setTimeout(() => {
      	map.jumpTo({center: cam.center, zoom:cam.zoom})
      }, 1000)
    });
</script>
</body>
</html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requestglobeGlobe related issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions