Can someone explain if I am doing this correctly please?

I've added some vectors to my map, now wish to zoom and centre the map based on the extends of those vectors.

I'm using the following code:

var dataExtent = borders.getDataExtent();

which, when examined in FireBug does return the following values:

CLASS_NAME    "OpenLayers.Bounds"
bottom         4187116.4740651
centerLonLat   null
left           -20037508.34
right          20037508.34
top            16852219.924326

And then I'm executing the following:

map.zoomToExtent(bounds, true);

(if it helps to determine if I'm indeed returning the correct values from the getDataExtend() function, the region should be Russia)

Do I need to convert those figures from the getDataExtent() to a different format?

I've also tried the following, which again didn't do anything:

    var dataExtent = borders.getDataExtent();

    var tl = new OpenLayers.Geometry.Point(dataExtent.top, dataExtent.left);
    tl = tl.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));

    var br = new OpenLayers.Geometry.Point(dataExtent.bottom, dataExtent.right);
    br = br.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));

    var bounds = new OpenLayers.Bounds(tl.x, tl.y, br.x, br.y);
    map.zoomToExtent(bounds, true);

2 Answers 2


When you create the bounds variable by instantiating the class OpenLayers.Bounds, it seems to me that you are not using the constructor method properly. According to the OpenLayers API (http://goo.gl/Nq0Ry):

OpenLayers.Bounds Construct a new bounds object. Coordinates can either be passed as four arguments, or as a single argument.

Parameters (four arguments)

left {Number} The left bounds of the box. Note that for width calculations, this is assumed to be less than the right value.

bottom {Number} The bottom bounds of the box. Note that for height calculations, this is assumed to be more than the top value.

right {Number} The right bounds.

top {Number} The top bounds.


Normally when ZoomToExtent does nothing you're passing in an invalid bounds. I'm not sure what projection you're using for your map but you could try adding the MousePosition control (if you haven't already) to make sure you're passing in something sensible.

  • You're right, having now added that control I can see the bounds are returning bad numbers. Am investigating a fix. Nov 7, 2012 at 11:28
  • Have now got my bounds 100% in line with the dataExtent variable but still not working at all. Is it because it goes over the dateLine? Nov 7, 2012 at 11:40
  • How could i use same in OpenLayers 3.x?
    – imdadhusen
    Mar 12, 2015 at 6:25

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.