2

in this map I have added a line of code to make it zoom to the vector layer:

map.zoomToExtent(vector_Layer.getDataExtent());

This zooms in to the maximum zoom at the point in the Gulf of Guinea where the equator meets the Greenwich meridian. Its not supposed to zoom here, its should show Scotland.

I've passed the same bit of code in the console in Chrome and that works fine. Perhaps, it wont work until after the page has loaded. Does anyone know why its not working properly when the page loads and does anyone know what I can do to fix it?

Thanks

1 Answer 1

2

You're right; occasionally scripts need to have the page elements loaded (and present, in the DOM) before they can act on them. I would start by trying one of these two approaches:

  1. Move your JS <script> tags to the bottom of the page, and out of the head. Put them just before the closing body tag, and see if that works. This way, your page has time to load (this is a very common practice).

  2. Alternatively, you can call this function in an onLoad event. You can read about how to use it here.

UPDATE:

Have a look at the following resources, if the above solutions don't work. It looks like there are a few other approaches to finding the "loaded" event for an OpenLayers map:

http://lists.osgeo.org/pipermail/openlayers-users/2012-April/024858.html https://gis.stackexchange.com/questions/10671/register-event-loadend-on-layer

Best of luck!

1
  • 1
    Thanks very much! the solution I've used is: vector_Layer.events.register("loadend", map, function(e) { map.zoomToExtent(vector_Layer.getDataExtent()); }); Jul 2, 2013 at 19:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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