I would like the get the center (coordinates) of the current view. I need it for the geoserver print module.
I've found the code var center= ol.extent.getCenter({});
but it unfortunately doesn't work. Furthermore I've found a code in the API (getview.getcenter())
, but it only shows the initial center and it is not changing when I explore on the map.
This is my code, it works fine except the center params… It gives back tha same values everytime.
var configs = {
"layout":"A4 portrait",
"srs":"EPSG:3857",
"units":"meter",
"dpi":300,
"outputFilename": "valami",
"layers":[
{baseURL: 'xy',
singleTile: false,
type: 'OSM',
maxExtent: [-20037508.3392,-20037508.3392,20037508.3392,20037508.3392],
tileSize: [256, 256],
extension: 'png',
opacity: 1,
resolutions: [156543.0339, 78271.51695, 39135.758475, 19567.8792375, 9783.93961875, 4891.969809375, 2445.9849046875, 1222.99245234375, 611.496226171875, 305.7481130859375, 152.87405654296876, 76.43702827148438, 38.21851413574219, 19.109257067871095, 9.554628533935547, 4.777314266967774, 2.388657133483887, 1.1943285667419434, 0.5971642833709717]
},
{"baseURL":"xy",
"opacity":1,
"singleTile":true,
"customParams":{},
"type":"WMS",
"layers":["mission:corine_ps"],
"format":"image/png",
"styles":[],
"overview":true},
{"baseURL":"xy",
"opacity":1,
"singleTile":true,
"customParams":{},
"type":"WMS",
"layers":["mission:utak_ps"],
"format":"image/png",
"styles":[],
"overview":true},
],
"pages":[
{
"center":[map.getView().getCenter()[1],map.getView().getCenter()[0]],
"mapTitle":"",
"comment":" ",
"scale": 1000000,
"rotation":0,
"icon":""}],
"legends": [
{
"classes": [
{
"icons": [
""
],
"name": "",
"iconBeforeName": true
}
],
"name": "utak"
}
]
};
var printUrl= 'xy?spec='+JSON.stringify(configs);
Best Answer
For OpenLayers 2 try
http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.getCenter
Or OpenLayers 3 use
http://openlayers.org/en/v3.5.0/apidoc/ol.Map.html#getView http://openlayers.org/en/v3.5.0/apidoc/ol.View.html#getCenter