openlayers-3

Openlayers 3.6: Getting the center of the current map view


I'm trying to get the current center of an Openlayers map in lat/lon coordinates. I've got the following event handler setup:

this.map.on('pointermove', function (e) {
    if (e.dragging) {
        console.log(that.map.getView().getCenter());
    }
});

This works, but I'm getting weird values. Here's an example:

[9318218.659044644, 3274618.6225819485]

Those are obviously not lat/lon values :) Do I need to transform this somehow? Thanks!


Solution

  • I'm not super familiar with openlayers but it sounds like the map is in different projection.

    Check out the following on spherical_mercator, transform, Open Layers projections

    UPDATED
    I've done a little more research, check out this example. Not sure what the projection your view is in. the map in this example is in ''EPSG:21781' if you go to a js console and enter map.getView().getCenter() you get [693230.7161150641, 179010.3389264635] but if you enter ol.proj.transform(map.getView().getCenter(), 'EPSG:21781', 'EPSG:4326') you get [8.658936030357363, 46.75575224283748] hope that helps.