georgephillips
georgephillips

Reputation: 3570

Google Maps Disable user panning on all events

In my google maps application I have a follow method which follows a moving marker. When it is following I want to allow zooming through all the usual methods (dblclick, dblleftclick, mousewheel and touch events) and I want to disable panning of any kind. The problem is that on zoom with mousewheel and dblclick the map gets panned to the position of the mouse. I can disable everything just fine but I want to allow zooming. I have solved the mousewheel problem by using the jquery mousewheel plugin and using the delta to change the zoom.

Is there some easy way to do this or do I have to write a listener for all the different touch and mouse events?

EDIT

I have already disable double click, mousewheel zooming and dragging but I want to have the double click functionality still there. I also want the touch events there but I want to have them zoom from the centre rather than from where the event happened. The real problem is replicating the events which google already handle but change the functionality a bit

var options = {
    disableDoubleClickZoom: true,
    draggable: false,
    scrollwheel: false,
    panControl: false
};

this.map = new google.maps.Map(document.getElementById('map'), options);

My ideal solution would be if there was a disableDoubleClickPan and disableScrollwheelPan or the draggable option actual prevents all dragging of any kind

EDIT

This is for all devices, desktop and mobile.

Upvotes: 24

Views: 22756

Answers (3)

GoldenLight
GoldenLight

Reputation: 199

Here is how I did it:

var options = {
    draggable: false,
    scrollwheel: false,
    panControl: false,
    maxZoom: Zoom,
    minZoom: Zoom,
    zoom: Zoom,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

As you can see, setting maxZoom and minZoom to the same value helps block user's double click event.

Upvotes: 19

georgephillips
georgephillips

Reputation: 3570

I have ended up going with a combination of options. Firstly I had to override desktop events which occurred to get my achieved result (touch, double click, double left click, and mouse wheel).

On a touch screen devise I paused all updates to the markers when there were more than two touches. this meant that any pinch event was not jumping around when the zoom operating.

On a normal web desktop device I disabled the zoom and double click events on the map and rewrote my own event handlers.

To distinquish between them I checked for the ontouchstart event in the window object.

function setDraggable(draggable) {
    if ("ontouchend" in document) {
        return;
    }
    var options = {
        draggable: draggable, 
        panControl: draggable, 
        scrollwheel: draggable 
    };
    this.map.setOptions(options);
},

The zoom_changed or idle events where not really an option for a few reasons:

  1. The idle event only gets called when the map is idle and with the amount of animation I was doing this never got called.
  2. The animation at each step recentered the map on the followed markers so the zoom_changed event would be recalling the recenter before an animation frame.
  3. due to the amount of animation the idea of not panning to the center is to reduce animation frames and improve performance.

Upvotes: 3

Andrew Leach
Andrew Leach

Reputation: 12983

While it's possible to argue that double-clicking the map or wheel-zooming the map need not take account of the mouse location (because you are acting on the map object rather than a location on the map), pinch-to-zoom is always location-dependent because you physically stretch or squash the map around a location. To alter that behaviour would be distinctly unintuitive.

In this case you should listen for zoom_changed or idle and then pan the map to recentre it, so the user can see what's going on.

You could even use those events to handle the default double-click or mousewheel behaviour so that it's obvious you are changing the level of control the user normally has.

Upvotes: 2

Related Questions