我能用&;z&;缩放地图吗?和“;a"在Openlayers3

Can I zoom the map with "z" and "a" in Openlayers3?

本文关键字:Openlayers3 quot 地图 缩放      更新时间:2023-09-26

如何在Openlayers3中使用"z"answers"a"缩放地图?目前我使用这个函数:map.getView().setZoom(map.getView().getZoom()+1)。但是我不知道当我按下"A"或"Z"时如何使它工作。

您可以使用keyCode。

当然,您可能需要获得map元素的焦点。

document.getElementById('map').focus();

例子:

<!DOCTYPE html>
<html>
  <head>
    <title>press 'A'or'Z example</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v3.20.1/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v3.20.1/build/ol.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js"></script>
    <style>
      .map {
        max-width: 566px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
      var raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });
      var map = new ol.Map({
        layers: [raster],
        target: 'map',
        controls: ol.control.defaults({
          attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
          })
        }),
        view: new ol.View({
		  projection: 'EPSG:4326',
          center:[104.07, 30.7],
          zoom: 2
        })
      });
      document.onkeydown=function(event){
           var e = event || window.event || arguments.callee.caller.arguments[0];
           if((e && e.keyCode==65)||(e && e.keyCode==90)){ // press 'A'or'Z
             map.getView().setZoom(map.getView().getZoom()+1)
           }
      };
    </script>
  </body>
</html>