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

Fix for issue 7438 by improving getRoundNum() function. #7469

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions debug/7438.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>Mapbox GL JS debug page</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel='stylesheet' href='/dist/mapbox-gl.css' />
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>

<body>
<div id='map'></div>

<script src='/dist/mapbox-gl-dev.js'></script>
<script src='/debug/access_token_generated.js'></script>
<script>

var map = window.map = new mapboxgl.Map({
container: 'map',
zoom: 12.5,
maxZoom: 24,
center: [-77.01866, 38.888],
style: 'mapbox://styles/mapbox/streets-v10',
hash: true
});

map.addControl(new mapboxgl.ScaleControl({ unit: 'nautical' }));

</script>
</body>
</html>
9 changes: 8 additions & 1 deletion src/ui/control/scale_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ function getRoundNum(num) {
d = d >= 10 ? 10 :
d >= 5 ? 5 :
d >= 3 ? 3 :
d >= 2 ? 2 : 1;
d >= 2 ? 2 :
d >= 1 ? 1 :
d >= 0.1 ? Math.round(d * 10) / 10 :
d >= 0.01 ? Math.round(d * 100) / 100 :
d >= 0.001 ? Math.round(d * 1000) / 1000 :
d >= 0.0001 ? Math.round(d * 10000) / 10000 :
d >= 0.00001 ? Math.round(d * 100000) / 100000 : Math.round(d * 1000000) / 1000000;
// Could go on forever of course, depending on the unit used. But this is good enough for 'imperial', 'metric' and 'nautical'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be generalized — looks like you can calculate the multiplier with Math.pow(10, Math.ceil(-Math.log(d) / Math.LN10)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought of that, but couldn't come up with the right formula. I'll try adding your formula.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Please check latest commit.


return pow10 * d;
}
13 changes: 13 additions & 0 deletions test/unit/ui/control/scale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ test('ScaleControl should change unit of distance after calling setUnit', (t) =>
t.match(contents, /mi/);
t.end();
});

test('ScaleControl should respect the maxWidth regardless of the unit and actual scale', (t) => {
const map = createMap(t);
const maxWidth = 100;
const scale = new ScaleControl({ maxWidth: maxWidth, unit: 'nautical' });
const selector = '.mapboxgl-ctrl-bottom-left .mapboxgl-ctrl-scale';
map.addControl(scale);
map.setZoom(12.5);

const el = map.getContainer().querySelector(selector);
t.ok(parseFloat(el.style.width, 10) <= maxWidth, 'ScaleControl respects maxWidth');
t.end();
});