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

fixes #6867 - map.getBounds() works with rotated map #6875

Merged
merged 2 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 64 additions & 0 deletions debug/bounds.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!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,
center: [-77.01866, 38.888],
style: 'mapbox://styles/mapbox/streets-v10',
hash: true
});

function geojson() {
const bounds = map.getBounds();
return {
type: 'Polygon',
coordinates: [[
bounds.getNorthEast().toArray(),
bounds.getNorthWest().toArray(),
bounds.getSouthWest().toArray(),
bounds.getSouthEast().toArray(),
bounds.getNorthEast().toArray()
]]
};
}

map.on('load', () => {
map.addSource('bounds', {
type: 'geojson',
data: geojson()
});

map.addLayer({
id: 'bounds',
type: 'fill',
source: 'bounds',
paint: {
'fill-color': "rgba(255,0,0,0.2)"
}
});

map.on('rotateend', () => {
map.getSource('bounds').setData(geojson());
});
});
</script>
</body>
</html>
20 changes: 8 additions & 12 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,17 @@ class Map extends Camera {
}

/**
* Returns the map's geographical bounds.
* Returns the map's geographical bounds. When the bearing or pitch is non-zero, the visible region is not
* an axis-aligned rectangle, and the result is the smallest bounds that encompasses the visible region.
*
* @returns {LngLatBounds} The map's geographical bounds.
* @returns {LngLatBounds}
*/
getBounds() {
const bounds = new LngLatBounds(
this.transform.pointLocation(new Point(0, this.transform.height)),
this.transform.pointLocation(new Point(this.transform.width, 0)));

if (this.transform.angle || this.transform.pitch) {
bounds.extend(this.transform.pointLocation(new Point(this.transform.size.x, 0)));
bounds.extend(this.transform.pointLocation(new Point(0, this.transform.size.y)));
}

return bounds;
return new LngLatBounds()
.extend(this.transform.pointLocation(new Point(0, 0)))
.extend(this.transform.pointLocation(new Point(this.transform.width, 0)))
.extend(this.transform.pointLocation(new Point(this.transform.width, this.transform.height)))
.extend(this.transform.pointLocation(new Point(0, this.transform.height)));
}

/**
Expand Down
9 changes: 8 additions & 1 deletion test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,16 @@ test('Map', (t) => {
t.test('rotated bounds', (t) => {
const map = createMap(t, { zoom: 1, bearing: 45, skipCSSStub: true });
t.deepEqual(
toFixed([[-49.718445552178764, 0], [49.7184455522, 0]]),
toFixed([[-49.718445552178764, -44.44541580601936], [49.7184455522, 44.445415806019355]]),
toFixed(map.getBounds().toArray())
);

map.setBearing(135);
t.deepEqual(
toFixed([[-49.718445552178764, -44.44541580601936], [49.7184455522, 44.445415806019355]]),
toFixed(map.getBounds().toArray())
);

t.end();
});

Expand Down