12

I am using openlayers3 and i am initializing my map like so:

var view = new ol.View({
    center: ol.proj.fromLonLat([5.8713, 45.6452]),
    zoom: 12,
});

var map = new ol.Map({
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        })
    ],
    target: 'map',
    controls: ol.control.defaults({
        attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
        })  
    }),
    view: view
});

Now how do i get the extent array , i tried the following code:

var extent = map.getView().getExtent();
console.log(extent);

But that does't seem to be the right code, i get an erros saying

Uncaught TypeError: map.getView(...).getExtent is not a function

can anybody explain , how do i go about getting the correct extent ?

1 Answer 1

40

Just do the following

map.getView().calculateExtent(map.getSize())
// Nowadays, map.getSize() is optional most of the time (except if you don't share view between different maps), so above is equivalent to the following
map.getView().calculateExtent()

considering that map is the variable referencing an instance of ol.Map like at this official example.

To correct you, it seems you didn't understood well the API documentation.

When you do a map.getView, it returns an ol.View instance and no getExtent method is available, hence your Uncaught TypeError: map.getView(...).getExtent is not a function error.

4
  • calculateExtent() is not even documented anywhere , i see a link , but i see no content explaining what exactly this function returns , see the link here openlayers.org/en/v3.0.0/apidoc/ol.View.html#calculateExtent , i have no idea what really what function returns . Commented Oct 31, 2015 at 22:03
  • 1
    You are using the v 3.0.0 documentation now outdated (for info, the last stable version is 3.10.1) so look at the latest doc about calculateExtent.
    – ThomasG77
    Commented Oct 31, 2015 at 22:07
  • what exactly does calculateExtent return ? Commented Nov 21, 2015 at 6:35
  • calculateExtent returns an array having bottom left longitude, latitude and top right longitude and latitude. somthing like [-40,-20,29,32]. You can save this array as var ex = map.getView().calculateExtent(map.getSize()); and call each values as ex[0], ex[1], etc.. Commented Jan 26, 2017 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.