maplibre / maplibre-gl-js

MapLibre GL JS - Interactive vector tile maps in the browser
https://maplibre.org/maplibre-gl-js/docs/
Other
6.49k stars 695 forks source link

Scale shows wrong numbers when zooming out. #4804

Open zerda-ocm opened 2 weeks ago

zerda-ocm commented 2 weeks ago

maplibre-gl-js version: 5.0.0-pre.1

browser: chrome, edge

Steps to Trigger Behavior

  1. add a scale to the map
    var scale = new maplibregl.ScaleControl({
    maxWidth: 80,
    unit: "imperial",
    });
    map.addControl(scale);

    2a. move or zoom slightly and observe the scale 2b. zoom out a lot and observe the scale

Link to Demonstration

https://jsfiddle.net/zLhtufqk/

Expected Behavior

Scale shows distance according to scale of the globe

Actual Behavior

The scale shows for example "300mi", after a move or zoom event (even a minor one), the scale jumps to for example "1000mi". Zooming further out, the scale shows smaller and smaller numbers until the whole earth is only 1 mile wide, Zooming in seem to work fine (presumably, because the projections gets closer to mercator.)

HarelM commented 2 weeks ago

Thanks for taking the time to report this bug. While I see the bug you have described, can you please make the stops to reproduce a bit defined for future readers?

russss commented 1 week ago

I just spotted this as well - you can see this behaviour on openinframap.org (which is using an unmodified ScaleControl), so I think it should be reproducible by simply adding the ScaleControl to any map using the globe projection.

The scale appears to be (at least roughly) correct at zoom levels above around 3.4, but when zooming out further, the distance shown decreases rather than increases.

(To be honest it's not hugely useful at those zoom levels, so I am tempted to add a minZoom option to the ScaleControl...)

HarelM commented 1 week ago

Looks like it is using unproject: https://github.com/maplibre/maplibre-gl-js/blob/c4b083fff7232630408c81b00757e55147138cf0/src/ui/control/scale_control.ts#L105 It might be that the scale location is outside the globe and thus getting unwanted values, which wasn't possible before, as the entire screen was filled with the map in 2D.