OpenLayers 3 Beginner’s Guide

Configuring ZoomToExtent and manipulate controls

Open sample in a new windows

<!doctype html>
<html>
  <head>
    <title>Simple example</title>
    <link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
    <link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
  </head>
  <body>
    <div id="map" class="map"></div>
    <script src="../assets/ol3/js/ol.js"></script>
    <script>
      var zoomToExtentControl = new ol.control.ZoomToExtent({
        extent: [-11243808.051695308, 4406397.202710291, -4561377.290892059, 6852382.107835932]
      });

      var osm_default = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      var map = new ol.Map({
        layers: [osm_default],
        target: 'map',
        view: new ol.View({
          center: ol.proj.transform([-83.43924243777822, 60.16139104246781], 'EPSG:4326', 'EPSG:3857'),
          zoom: 3
        })
      });

      map.addControl(zoomToExtentControl);
      var controls = map.getControls();
      var attributionControl;
      controls.forEach(function (el) {
        console.log(el instanceof ol.control.Attribution);
        if (el instanceof ol.control.Attribution) {
          attributionControl = el;
        }
      });
      map.removeControl(attributionControl);

    </script>
  </body>
</html>