How to zoom in smoothly on a marker in Google Maps?

后端 未结 6 1830
孤独总比滥情好
孤独总比滥情好 2020-12-22 22:28

I\'d like to be able to zoom in smoothly on a marker in Google Maps. If one just sets the zoom on double click, the map is suddenly on that zoom level, without any smooth tr

6条回答
  •  悲&欢浪女
    2020-12-22 23:24

    As luck would have it, I wanted to achieve the same effect recently, and found a solution, which I made a post about. Basically, just setting a timeout for each transition isn't enough, because it could easily result in a 'start-stop' kind of zoom if Google's zoom effect isn't finished yet, or has long finished.

    As Martin mentioned, there are some disadvantages to this, which I won't reiterate. Whether you use this in the end is your choice, and depends largely on your users' CPU power and/or browser. It is a nice effect though, and sure to impress some, when used wisely.

    My solution was the following:

    // example marker:
    var marker = new google.maps.Marker({
        map: map, 
        position: new google.maps.LatLng(-20.3,30.3)
    });
    
    
    // add the double-click event listener
    google.maps.event.addListener(marker, 'dblclick', function(event){
        map = marker.getMap();    
        map.setCenter(overlay.getPosition()); // set map center to marker position
        smoothZoom(map, 12, map.getZoom()); // call smoothZoom, parameters map, final zoomLevel, and starting zoom level
    });
    
    
    // the smooth zoom function
    function smoothZoom (map, max, cnt) {
        if (cnt >= max) {
            return;
        }
        else {
            z = google.maps.event.addListener(map, 'zoom_changed', function(event){
                google.maps.event.removeListener(z);
                smoothZoom(map, max, cnt + 1);
            });
            setTimeout(function(){map.setZoom(cnt)}, 80); // 80ms is what I found to work well on my system -- it might not work well on all systems
        }
    }  
    

    Basically what it comes down to is adjusting the zoom level by one, listening for the zoom_changed event, waiting 80ms before adjusting the zoom-level by one again, etc. What's nice about this is that the zoom_changed event seems to be called after the smooth transition provided by Google Maps, but before the actual images are loaded, so it doesn't waste bandwidth too much.

    The 80ms in the timeout is also a magic number I came up with - you would be well-advised to do a more thorough test and see what works on different systems and browsers, and perhaps change the algorithm slightly based on your findings or for different systems.

    It's probably also not necessary to add and remove the listener every time, but you can make that small improvement yourself if you so wish.

提交回复
热议问题