2

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);
  • Are you accessing getCenter in some map change event callback? – John Powell May 18 '15 at 12:22
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. – PolyGeo Feb 18 '19 at 4:06
3

For OpenLayers 2 try

map.getCenter()

http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/Map-js.html#OpenLayers.Map.getCenter

Or OpenLayers 3 use

map.getView().getCenter()

http://openlayers.org/en/v3.5.0/apidoc/ol.Map.html#getView http://openlayers.org/en/v3.5.0/apidoc/ol.View.html#getCenter

  • Thank You for the answer, but Your solution is returns the initial center, and this is a predefined value. I need the current center, so I would like to get the actual view's center coordinates. – user52288 May 19 '15 at 15:30
  • Which version of Openlayers are you using? – Guy May 20 '15 at 10:33
  • Openlayers 3.3.0 – user52288 May 24 '15 at 21:31
  • @user52288 using the map.getView().getCenter() should get the current center of the ol.Map object called "map". Are you sure you're calling the view correctly? It would help if you could include a snippet or link to some code. – Guy Jun 1 '15 at 15:32
  • i've written my code, could you check it? thanks!! – user52288 Jun 4 '15 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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