How to load layers depending on zoom level?

后端 未结 2 596
北恋
北恋 2021-02-05 23:59

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

2条回答
  •  青春惊慌失措
    2021-02-06 00:31

    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);
      }
    }
    

提交回复
热议问题