Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen for user generated zoom in Google Maps?

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, I can't tell if it is a user-generated event or a zoom change caused by something like fitBounds(). Looking for the best way to do this.

I've tried the following things, none of which seem to work:

1) Looked for event information on zoom_changed. There appears to be none.

2) Add listeners for mouseover and mouseout that let me set a flag to see if the user is in the map bounds, and check the flag on zoom_changed. This doesn't work because the map does not consider the zoom buttons as part of the map frame (in other words, hovering over zoom buttons triggers the mouseout event).

3) Add a normal (non-gMap) listener to the zoom buttons. However, I can't find a definitive CSS selector that will allow me to grab just the buttons.

4) Looked for a function in the gMaps API that would let me do something like getZoomElements(), and then I could set listeners using that.

The weird thing is I can clearly do what I want if I add a custom control to the map. It seems very odd that they would force me to do that instead of having a hook into the default zoom controls.

like image 775
LAW Avatar asked Apr 10 '12 23:04

LAW


People also ask

Can you zoom in on Google Maps?

Zoom in the mapYou can zoom in and out of the map with one hand. Double tap a spot on the map, and then: Drag down to zoom in. Drag up to zoom out.

How do I check the zoom level on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

Where is the zoom button on Google Maps?

The Zoom control displays "+" and "-" buttons for changing the zoom level of the map. This control appears by default in the bottom right corner of the map.

How do I zoom in on Google Maps without street view?

You can also use the arrows to the left and right of the compass. To zoom in or out, scroll with your mouse or two-finger zoom with a touchpad. You can also use + and - below the compass. To position your Street View north, go to the bottom right-hand corner and click the compass.


2 Answers

I wouldn't just hook in to the +/- buttons (or buttons on your own custom control for that matter). The user can change the zoom on the map with the mouse wheel, or by double-clicking on the map. Plus, you'd be relying on implementation detail rather than documented API, which is a major no-no.

This really means the only reliable, documented way to detect a change in zoom is to listen to the zoom_changed event of the Map.

If your event handler can't determine whether the event came from user action or an API call, there's two approaches:

  • Set a flag before calling an API function so that you know you can ignore this change.
  • Can you re-architect your app such that it does not matter whether a zoom change came from code or the user?
like image 73
josh3736 Avatar answered Sep 18 '22 14:09

josh3736


I solved this issue by creating Custom Zoom buttons for my Map.

Here is the code from my project:
Edit: Removed unnecessary and self explanatory common code

your zoomControl function:

function zoomControl(map, div) {

  var controlDiv = div,
      control = this;

  // Set styles to your own pa DIV
  controlDiv.style.padding = '5px';

  // Set CSS for the zoom in div.
  var zoomIncrease = document.createElement('div');
  zoomIncrease.title = 'Click to zoom in';
  // etc.

  // Set CSS for the zoom in interior.
  var zoomIncreaseText = document.createElement('div');
  // Use custom image
  zoomIncreaseText.innerHTML = '<strong><img src="./images/plusBut.png" width="30px" height="30px"/></strong>';
  zoomIncrease.appendChild(zoomIncreaseText);

  // Set CSS for the zoom out div, in asimilar way
  var zoomDecreaseText = document.createElement('div');
  // .. Code .. Similar to above

  // Set CSS for the zoom out interior.
  // .. Code ..

  // Setup the click event listener for Zoom Increase:
  google.maps.event.addDomListener(zoomIncrease, 'click', function() {
      zoom = MainMap.getZoom();
      MainMap.setZoom(zoom+1);
      // Other Code parts
  });

  // Setup the click event listener for Zoom decrease:
  google.maps.event.addDomListener(zoomDecrease, 'click', function() {
      zoom = MainMap.getZoom();
      MainMap.setZoom(zoom-1);
  });
}

your initialize function:

function initializeMap() {

    var latlng = new google.maps.LatLng(38.6, -98);
    var options = {
        zoom : 5,
        center : latlng,
        mapTypeId : google.maps.MapTypeId.ROADMAP,  
        // Other Options
    };
    MainMap = new google.maps.Map(document.getElementById("google-map-canvas"),
            options);

    // The main part - Create your own Custom Zoom Buttons
    // Create the DIV to hold the control and call the zoomControl()
    var zoomControlDiv = document.createElement('div'), 
        zoomLevelControl = new zoomControl(MainMap, zoomControlDiv);

    zoomControlDiv.index = 1;
    MainMap.controls[google.maps.ControlPosition.RIGHT_TOP].push(zoomControlDiv);
}

Hope it helps

like image 39
Premshankar Tiwari Avatar answered Sep 18 '22 14:09

Premshankar Tiwari