Skip to content

Commit 0bf0d76

Browse files
committed
feat: Add a map.corners() utility function
This gets the current corners of the map as a list of four points.
1 parent 4aaa9a6 commit 0bf0d76

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/map.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,27 @@ var map = function (arg) {
14401440
return m_this;
14411441
};
14421442

1443+
/**
1444+
* Get the corners of the map. Since the map can be rotated, this is
1445+
* necessarily not the same as the overall bounds, which is the orthogonal
1446+
* bounding box.
1447+
*
1448+
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
1449+
* gcs, `null` to use the map gcs, or any other transform. If setting the
1450+
* bounds, they are converted from this gcs to the map projection. The
1451+
* returned bounds are converted from the map projection to this gcs.
1452+
* @returns {geo.geoPosition[]} The corners of the map in the order
1453+
* upper-left, upper-right, lower-right, lower-left.
1454+
*/
1455+
this.corners = function (gcs) {
1456+
return [
1457+
m_this.displayToGcs({x: 0, y: 0}, gcs),
1458+
m_this.displayToGcs({x: m_width, y: 0}, gcs),
1459+
m_this.displayToGcs({x: m_width, y: m_height}, gcs),
1460+
m_this.displayToGcs({x: 0, y: m_height}, gcs)
1461+
];
1462+
};
1463+
14431464
/**
14441465
* Get the center zoom level necessary to display the given bounds.
14451466
*

tests/cases/map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ describe('geo.core.map', function () {
266266
bottom: -128 * units,
267267
width: 256 * units,
268268
height: 256 * units})).toBe(true);
269+
expect(closeToEqual(m.corners()[0], {x: -180, y: 85.05}));
270+
expect(closeToEqual(m.corners(null)[0], {x: -128 * units, y: 128 * units}));
269271
m.ingcs('EPSG:3857');
270272
expect(m.ingcs()).toBe('EPSG:3857');
271273
expect(closeToEqual(m.bounds(), {

0 commit comments

Comments
 (0)