Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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();
    console.log(dataExtent);

    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);
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
You're right, having now added that control I can see the bounds are returning bad numbers. Am investigating a fix. –  JasonMHirst Nov 7 '12 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? –  JasonMHirst Nov 7 '12 at 11:40
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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