【问题标题】:activate Google maps keyboard bindings immediately after page load页面加载后立即激活谷歌地图键盘绑定
【发布时间】:2013-04-12 11:20:44
【问题描述】:

是否可以在页面加载后立即激活 Google Maps (StreetView) 的键盘绑定?我找到了一个工作示例here,不幸的是它使用了不再支持的 Google Maps API v2。如果您嵌入了普通的 Google 地图街景视图,则在您最初单击地图之前,键盘绑定不起作用(请参阅here)。

是否有可能在 v3 中执行此操作?我已经尝试过thisthis 没有成功,因为那不适用于街景。

更新:使用键盘绑定,我特别想用箭头键在地图上移动

谢谢

问候

【问题讨论】:

    标签: google-maps google-maps-api-3 google-street-view


    【解决方案1】:

    好的,找到了解决方案并为此创建了Gist

    【讨论】:

      【解决方案2】:

      @ChristianB 提出的解决方案对我不起作用。可能是因为它适用于旧版本的 Google Maps API(我的是 v3),或者它使用了不同的渲染模式(我的是 webgl,它使用画布)。

      我做的是等待position_changed事件触发,然后在canvas元素上设置一个tabIndex属性,然后在canvas元素上触发focus()。之后,键盘绑定就起作用了。

      代码:

      var panorama = new google.maps.StreetViewPanorama({
        ...
        //forcing webgl 
        mode: 'webgl'
      });
      
      //Set an event listener for position_changed, 
      //this will be triggered the first time the panorama is loaded, 
      //and every time the position changes
      google.maps.event.addListener(panorama, 'position_changed', function() {
        //This is how to get the canvas in my current version of the Google Maps API, 
        //note that this might change in the future.
        var canvas = document.querySelector('canvas.widget-scene-canvas');
      
        //first set a tabindex on the canvas, 
        //without it focus will not make the canvas the active element
        canvas.setAttribute("tabindex", "-1");
        canvas.focus();
      
        //To test that it worked you can check that document.activeElement is the canvas
        console.log(document.activeElement);
      });
      

      您可能希望将代码放在一个单独的函数中,并在画布失去焦点时调用它。

      在 Google Chrome v55 中测试的代码。

      【讨论】:

      • 是的,我的解决方案肯定是针对旧的 GMaps API
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 2016-03-31
      • 2019-07-27
      • 2011-03-31
      相关资源
      最近更新 更多