Friday, 2 December 2016

Getting extent in OpenLayers 3?


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 ?



Answer




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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...