Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable zooming dragging in Google maps by clicking on a button

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

<script type="text/javascript">
   var map;

  function initialize() {
var uluru = new google.maps.LatLng(21, 57);
map = new google.maps.Map(document.getElementById("map"), {
  zoom: 6,
  center: uluru,
  mapTypeId: google.maps.MapTypeId.HYBRID
});
}


function disable(){

}

</script>


<body onload="initialize()" >

   <input type="button" id="next" value="disableZoomDrag" onclick="disable()">

</body>
like image 615
Sami Al-Subhi Avatar asked Nov 20 '11 19:11

Sami Al-Subhi


People also ask

How can we enable or disable the zoom in the map?

The Maps API provides built-in zoom controls that appear in the bottom right hand corner of the map. These are disabled by default, but can be enabled by calling UiSettings. setZoomControlsEnabled(true) .

How do I change the default zoom on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

Which method is used to display the controls for zooming options in map?

Important Methods of Zoom Controls ZoomControl zoomControls = (ZoomControls) findViewById(R. id. simpleZoomControl); show(): This method is used to show the zoom controls on the App UI.


2 Answers

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.

like image 83
ScottE Avatar answered Oct 12 '22 07:10

ScottE


@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.

like image 20
Jasper Avatar answered Oct 12 '22 05:10

Jasper