Skip to content

Commit

Permalink
feat: Add a map.corners() utility function
Browse files Browse the repository at this point in the history
This gets the current corners of the map as a list of four points.
  • Loading branch information
manthey committed Dec 10, 2024
1 parent 4aaa9a6 commit 0bf0d76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,27 @@ var map = function (arg) {
return m_this;
};

/**
* Get the corners of the map. Since the map can be rotated, this is
* necessarily not the same as the overall bounds, which is the orthogonal
* bounding box.
*
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
* gcs, `null` to use the map gcs, or any other transform. If setting the
* bounds, they are converted from this gcs to the map projection. The
* returned bounds are converted from the map projection to this gcs.
* @returns {geo.geoPosition[]} The corners of the map in the order
* upper-left, upper-right, lower-right, lower-left.
*/
this.corners = function (gcs) {
return [
m_this.displayToGcs({x: 0, y: 0}, gcs),
m_this.displayToGcs({x: m_width, y: 0}, gcs),
m_this.displayToGcs({x: m_width, y: m_height}, gcs),
m_this.displayToGcs({x: 0, y: m_height}, gcs)
];
};

/**
* Get the center zoom level necessary to display the given bounds.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ describe('geo.core.map', function () {
bottom: -128 * units,
width: 256 * units,
height: 256 * units})).toBe(true);
expect(closeToEqual(m.corners()[0], {x: -180, y: 85.05}));
expect(closeToEqual(m.corners(null)[0], {x: -128 * units, y: 128 * units}));
m.ingcs('EPSG:3857');
expect(m.ingcs()).toBe('EPSG:3857');
expect(closeToEqual(m.bounds(), {
Expand Down

0 comments on commit 0bf0d76

Please sign in to comment.