Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable scrollwheel zooming on a map upon click using Google Maps API

I am using the Google Maps API, and wanted to be able to scroll past the map on a mobile device and not have the map zoom when scrolling down a webpage using the scrollwheel.

Here's my code:

var mapOptions = {
  center: new google.maps.LatLng(51.605139, -2.918567),
  zoom: 15,
  scrollwheel: false,
  draggable: false,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};

This is fine but what I would like to achieve is to enable scrollwheel zooming only when the map was clicked.

(So on a webpage, when I click the map, I can zoom on it, or a mobile device, when I tap the screen on, then I can pinch and zoom/drag the map around.)

Can I add an event listener to activate draggable only upon double click or single click?
Is it possible using the API?


EDIT

I have tried the following code, but it doesn't seem to work:

google.maps.event.addListener(map, 'click', function (event) {
  var mapOptions = {
    draggable: true,

  };
});
like image 462
Richlewis Avatar asked Mar 10 '13 21:03

Richlewis


People also ask

How do I zoom out on Google Maps with mouse?

Many of you have no clue, how to Zoom in and out in Google MAPS with your Mouse. I'll show you! :-) just DOUBLE PRESS the left Mousebutton and HOLD ......and while you re holding your mousbutton, you just can move your mouse to zoom in and out.


2 Answers

This should work:

        google.maps.event.addListener(map, 'click', function(event){
          this.setOptions({scrollwheel:true});
        });
like image 181
Dr.Molle Avatar answered Oct 19 '22 02:10

Dr.Molle


google.maps.event.addListener(map, 'mouseout', function(event){
 this.setOptions({scrollwheel:false});  
});

this is a handy little consideration in addition to Dr.Molle's answer.

just as it appears, this will re-disable scrollwheel zooming when the mouse leaves the map.

like image 43
mrcni Avatar answered Oct 19 '22 00:10

mrcni