How to listen for user generated zoom in Google Maps?

前端 未结 5 2119
借酒劲吻你
借酒劲吻你 2021-02-04 06:24

I want to know when a Google Maps zoom_changed event is fired specifically by a user interaction with the +/- zoom buttons. If I use a general event listener for zoom_changed,

5条回答
  •  广开言路
    2021-02-04 07:01

    I was looking to do the same thing. I was hoping to find that there was a way built into the Google Maps API, but at a minimum, you should be able to store the starting zoom level as a variable when you initialize the map. Then, compare the result of getZoom() to it to know whether it was a zoom in or a zoom out.

    For example:

    map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 11 });
    var previous_zoom = 11;
    
    google.maps.event.addListener(map,'zoom_changed',function(){
      if(map.getZoom() > previous_zoom) {
        alert('You just zoomed in.');
      }
      previous_zoom = map.getZoom();
    }
    

提交回复
热议问题