Sombriks Sombriks - 10 months ago 78
Javascript Question

how to get the map bounds?

I'm migrating from leaflet and back then if i needed the map bound i used the following code:

var b = map.getBounds();
$scope.filtromapa.lat1 = b.getSouth();
$scope.filtromapa.lat2 = b.getNorth();
$scope.filtromapa.lng1 = b.getWest();
$scope.filtromapa.lng2 = b.getEast();

Those values where valid latitude/longitude positions therefore i could send it to my backend and query any position inside this area.

How can i do that using openlayers?

for now all i have is:

var b = map.getView().calculateExtent(map.getSize());

however the positions aren't valid latitude/longitude positions.

I'm using openlayers 3.19.1

Answer Source

Following the answer at (and its assumptions)

var currentExtent = map.getView().calculateExtent(map.getSize()),
    TLpoint = ol.extent.getTopLeft( currentExtent ),
    BRpoint = ol.extent.getBottomRight( currentExtent ),
    TLcoords = ol.proj.transform( TLpoint, 'EPSG:3857', 'EPSG:4326' ),
    BRcoords = ol.proj.transform( BRpoint, 'EPSG:3857', 'EPSG:4326' );