10

I want to control the zoom in OpenLayers.

When the zoom is 3 I want to load KML1 and when the zoom is 4 i want to load KML2.

Could you please advise me how I can control the zoom-event?

ischas
  • 169
  • 2
  • 12
user1365697
  • 5,819
  • 15
  • 60
  • 96

2 Answers2

7

As j_freyre mentioned you should register a function, which changes the visibility of your KML-layers, to the "zoomend"-event. In your case it has to look like this:

map.events.register("zoomend", map, zoomChanged);

zoomChanged()
{
  zoom = map.getZoom();
  if (zoom == 3)
  {
    kml1.setVisibility (true);
    kml2.setVisibility (false);
  }
  else if (zoom == 4)
  {
    kml1.setVisibility (false);
    kml2.setVisibility (true);
  }
}
ischas
  • 169
  • 2
  • 12
  • It's more efficient to store the `getZoom` value in a variable like `zoom = map.getZoom();` and then check it out on the if statements like `if (zoom == 3)`. In this way you're avoiding to call `getZoom` each time. – Mahdi Apr 11 '13 at 09:02
3

a way to accomplish that is to register an event on event "zoomend" on your map with something like

map.events.register(type, obj, listener);

You can find more info there: http://dev.openlayers.org/releases/OpenLayers-2.11/doc/apidocs/files/OpenLayers/Map-js.html

Maybe you should also load both of your kml and then hide / show the correct layer depend on the zoom level.

j_freyre
  • 4,623
  • 2
  • 30
  • 47
  • I want that every change of the zoom to do something.Th event that I inserted is zoom_changed but it is not working.maybe you have the list of all the events – user1365697 May 24 '12 at 06:44