8

It seems quite simple thing to me, but I get an error.

map declaration

map = new OpenLayers.Map('map_element', {...
projection : _projObj.mercator,
displayProjection : _projObj.wgs84
}

layer declaration

uses_layer = new OpenLayers.Layer.Vector("US Layer", {
 strategies : [new OpenLayers.Strategy.Fixed()],
 projection : _projObj.wgs84,
 visibility : true,
 protocol : new OpenLayers.Protocol.WFS({
 version : '1.0.0',
 url : '[...]wfs.map&service=WFS',
 featureType : USType
 })
});

center to layer extent

After the layer gets successfully loaded, I want to zoom to the vector layer extent:

map.zoomToExtent(uses_layer.getDataExtent());

and I get:

Uncaught TypeError: Cannot call method 'getCenterLonLat' of null
OpenLayers.Map.OpenLayers.Class.zoomToExtentOpenLayers.js:512
addSelectedLayermap.js:87
(anonymous function)46.105.19.68:36
f.event.dispatchjquery-1.7.2.min.js:3
f.event.add.h.handle.i

When I pass the same commands from JS debug command line (embedded in Chrome), the map get zoomed correctly; nonetheless,

uses_layer.getDataExtent();

from command line, gives me

bottom: 5134852.1720288
centerLonLat: null
left: 1744085.9879363
right: 1787837.9987162
top: 5150209.4139235

Now, centerLonLat is null, but in the docs it is said that the only reqested value is a bound with coordinates array, that IMHO fits with the uses_layer.getDataExtent(); request.

Any hint? Thanks :)

asked Aug 12, 2012 at 10:22

1 Answer 1

4

Is it possible that your layer hasn't finished downloading when you call getDataExtent()? while in the debugger it is going more slowly and the download has finished.

This previous question OpenLayers load WFS vector layer: how to check whether all features are returned from server? should help.

answered Aug 12, 2012 at 14:20
1
  • 4
    Thanks, issues solved. I've implemented the load check as follows: uses_layer.events.register('loadend', uses_layer, function(evt){map.zoomToExtent(uses_layer.getDataExtent())}) Commented Aug 12, 2012 at 15:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.