Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable drag inertia / momentum on Google Maps V3

Is there a way to disable drag inertia on Google Maps V3? Seems like it should be a MapOption, but I can't find any way to do this.

like image 205
pixelfreak Avatar asked Aug 10 '11 23:08

pixelfreak


3 Answers

I ran into this same problem today, with some custom Div's floating above the map needing to be re-positioned on Map Movement. My repositioning worked fine as long as the user came to a complete stop after dragging before letting the mouse go (so there would be no momentum), but if one just dragged quickly and released the div's would end up a bit off.

To fix this, I hooked into the drag event and the idle event:

var map = /* All the map config */
var stickyCenter = map.getCenter();
/* ... Code ... */
google.maps.event.addListener(map, 'drag', function(){ stickyCenter = map.getCenter(); });
google.maps.event.addListener(map, 'idle', function() { map.setCenter(stickyCenter); });

What happens is that after you've dragged and the map has come to a stop (after the momentum is done) the map 'snaps' back into place.

If the snapping is too sudden, one could probably panTo or animate the movement in some way. Hope that helps, it's not perfect, but it's a way to reverse the momentum from a drag event.

like image 167
EdgeCaseBerg Avatar answered Nov 15 '22 13:11

EdgeCaseBerg


Use the undocumented option disablePanMomentum e.g.:

new google.maps.Map(document.getElementById(id), {
    disablePanMomentum: true,
    backgroundColor: 'none',
    disableDefaultUI: true,
    center: {
        lat: 40.674,
        lng: -73.945
    },
    zoom: 2,
    ...
like image 32
Orwellophile Avatar answered Nov 15 '22 13:11

Orwellophile


This cannot be done with Maps API V3 at this time. Consider filing a feature request here:

http://code.google.com/p/gmaps-api-issues/

like image 2
plexer Avatar answered Nov 15 '22 11:11

plexer