使用双击或单击事件平移控件和打开/关闭可拖动? Google 地图 v3

Pan controls & toggling on / off draggable using double or single click events? Google Maps v3

有没有人知道如何切换点击事件来打开和关闭 'draggable',这样点击一次它就可以拖动,再次点击它就变成非活动状态?

在移动设备上持续保持可拖动可能会成为一个问题。

map.addListener('click', function() {  
    map.set('draggable', true);  
});  

以上效果很好,但我如何让它在下一次点击事件时切换回关闭状态?或者甚至可以通过双击(点击两次)打开并以同样的方式再次关闭?

.

Quick Note: My original question was about the Pan control feature on Google Maps V3, but as the first response shows it has since been depreciated.

对于平移控件,您应该使用 panControl ..但是从 v3.22 开始似乎已被 google 弃用 https://developers.google.com/maps/articles/v322-controls-diff#overview-map-control-deprecated

function initMap() {
map = new google.maps.Map(document.getElementById('footer-map'), {
  center: {lat: -37.111, lng: 144.888},
  zoom: 14,
  scrollwheel: false,
  draggable: false, 
  disableDoubleClickZoom: true,
  panControl:true,
  navigationControl: true,
  navigationControlOptions: {
    style: google.maps.NavigationControlStyle.ZOOM_PAN
  }
});
};

设置适当的 window 作用域变量

var isDraggable = true; 

你的监听器测试 isDraggable 的状态并改变

map.addListener('click', function() {  
  if (isDraggable ) then {
     map.set('draggable', false);  
    isDraggable = false; 
   } else{
     map.set('draggable', true);  
    isDraggable = truen; 
  }
});