diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa9ce7..e933d76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Include transform and transformExtent OL methods. #171 +- Add getCurrentViewExtentCoordinates instance function. #171 ## [v2.1.0] - 2022-11-15 diff --git a/src/instance/instance.js b/src/instance/instance.js index eda45bd..418585e 100644 --- a/src/instance/instance.js +++ b/src/instance/instance.js @@ -8,7 +8,7 @@ import defaults from './defaults'; // Import instance methods. import addLayer, { getLayerByName } from './methods/layer'; import addPopup from './methods/popup'; -import { transform, transformExtent } from './methods/projection'; +import { getCurrentViewExtentCoordinates, transform, transformExtent } from './methods/projection'; import { zoomToVectors, zoomToLayer } from './methods/zoom'; import { addBehavior, attachBehavior, attachBehaviorsByWeight } from './methods/behavior'; import { measureGeometry } from '../utils/measure'; @@ -58,6 +58,7 @@ const createInstance = ({ target, options = {} }) => { attachBehavior, attachBehaviorsByWeight, measureGeometry, + getCurrentViewExtentCoordinates, transform, transformExtent, }; diff --git a/src/instance/methods/projection.js b/src/instance/methods/projection.js index 27d8993..e7c225a 100644 --- a/src/instance/methods/projection.js +++ b/src/instance/methods/projection.js @@ -1,2 +1,17 @@ +import { transformExtent } from 'ol/proj'; + // Export OpenLayers transform functions -export { transform, transformExtent } from 'ol/proj'; \ No newline at end of file +export { transform, transformExtent } from 'ol/proj'; + +/** + * Returns the map's current view extent in the specified projection coordinates. + * @param {import('ol/proj').ProjectionLike} source Source projection-like. + * Defaults to 'EPSG:3857'. + * @param {import('ol/proj').ProjectionLike} destination Destination projection-like. + * Defaults to 'EPSG:4326'. + * @return {import('ol/extent').Extent} The transformed extent. + */ +export function getCurrentViewExtentCoordinates(source = 'EPSG:3857', destination = 'EPSG:4326') { + const extent = this.map.getView().calculateExtent(this.map.getSize()); + return transformExtent(extent, source, destination); +}