2016-10-03 61 views

回答

0

你可以通过keyCode来实现。

当然,您可能需要获取地图元素的焦点。

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({ 
 
\t \t 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>

相关问题