disable zooming dragging in Google maps by clicking on a button

前端 未结 2 873
别那么骄傲
别那么骄傲 2020-12-12 18:54

I want to add codes inside disable() function to disable dragging and zooming in Google maps API v3 by clicking on \'disable\' button.



        
相关标签:
2条回答
  • 2020-12-12 19:35

    You can use the setOptions() method on the map object:

    map.setOptions({draggable: false, zoomControl: false, scrollwheel: false, disableDoubleClickZoom: true});
    

    If this doesn't prevent zooming, you could always set the minimum and maximum zoom to the current zoom level.

    There is also the disableDefaultUI option, which probably takes all of these events into account, but it might disable clicking on existing elements.

    0 讨论(0)
  • 2020-12-12 19:39

    @ScottE's answer pointed me in the right direction of using map.setOptions(). However, from the API documentation, I found that there is a more elegant set of options to set. Perhaps these are newer than the answer, but they work pretty well for me.

    function disablePanningAndScrolling()
    {
        map.setOptions({
            zoomControl: false,
            gestureHandling: 'none'
        });
    }
    

    This completely disables zooming and panning.

    To return things to normal, just set the properties back to their defaults:

    function enablePanningAndScrolling()
    {
        map.setOptions({
            zoomControl: true,
            gestureHandling: 'greedy' // or 'cooperative'*
        });
    }
    

    *: the default is greedy if the page isn't scrollable, cooperative when it is. Pick whichever was the situation in your application.

    0 讨论(0)
提交回复
热议问题