[GIS] can I get map’s bounding box at any time, after user clicks button

extentsopenlayers-2

I use openlayers. I am creating a form that the user can insert a bounding box to the database.

In my page I have the form and a map, so the user can preview what he entered.

So I thought it would be nice if there was also the other way around :

User hits a button like "Get current bounding box", and that gets the bounding box from the map at any given time and transport it to the form. The numbers of the bounding box that is.
I'm talking about the bounding box that the users see, not the whole map's box.

After that, user can hit "Save" and submit the form, or can clear the form, or edit it, or zoom/pan the map and hit "Get current bounding box" again.

I think you got the idea…

Is that possible?

How can I get map's bounding box at any given time?

Thanks in advance

Best Answer

I know this is old,

but I keep getting wrong information due to the breaking change of openlayer 2 to 3.

So

Openlayer 2 :

map.getExtent().toBBOX();
// return Simple String representation of bounds object.  (e.g.  “5,42,10,45”)

Openlayer 3 :

map.getView().calculateExtent(map.getSize())
// return an array of coordinate [-9352936.233159352, 4617690.331601141, -4837648.098297421, 6897348.263178239]

Openlayer3 has a really bad documentation. Googling for Ol api always refer you to an old version of the API. The API has key feature marked has "not stable". There is undocumented features such as calculateExtent and obviously, the breaking change from 2 to 3 makes OpenLayer, in my opinion, a pretty bad library.

Just before you start a new project with openlayer I suggest to anyone to consider using leaflet.js which is a pretty solid, usable and stable library with wonderful one-page documentation.

Related Question