Google Maps Disable user panning on all events

前端 未结 3 868
灰色年华
灰色年华 2020-12-29 04:35

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, dbl

相关标签:
3条回答
  • 2020-12-29 05:06

    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.

    0 讨论(0)
  • 2020-12-29 05:08

    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.

    0 讨论(0)
  • 2020-12-29 05:10

    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.
    0 讨论(0)
提交回复
热议问题