From 0bf0d767a435622dd12519b0c36cd3a43ad2ff84 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Tue, 10 Dec 2024 14:33:44 -0500 Subject: [PATCH] feat: Add a map.corners() utility function This gets the current corners of the map as a list of four points. --- src/map.js | 21 +++++++++++++++++++++ tests/cases/map.js | 2 ++ 2 files changed, 23 insertions(+) diff --git a/src/map.js b/src/map.js index 7a189c5c33..6a932dfb2e 100644 --- a/src/map.js +++ b/src/map.js @@ -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. * diff --git a/tests/cases/map.js b/tests/cases/map.js index bb0f5297d1..c8a59453ce 100644 --- a/tests/cases/map.js +++ b/tests/cases/map.js @@ -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(), {