RK* - rikkertkoppes.com

thoughts

Google maps zooming investigated

The google maps API provides two methods to zoom your map through script, namely, zoomIn and zoomOut. Sadly, these methods do not use the smooth zooming you get when zooming with the mouse.

Still I wanted to create a method that allows one to "hop" to another place on the map, much like the animation seen in Google Earth. Therefore, I would first (smoothly) zoom out until source and destination places where in view and than (smoothly) zoom in to the destination. Here is the story.

Dissecting zoom methods

Suppose I have a working Google Maps implementation with smooth scrolling enabled. See for instance this example. Now make sure you have Firebugs console before you.

We start investigating what the zoomIn and zoomOut methods are about, type:

map.zoomIn.toSource();
map.zoomOut.toSource();

What you get back is the following:

"(function (a, b, c) {if (this.Ef && c) {this.ak(1, true, a, b);} else {this.sp(1, true, a, b);}})"
"(function (a, b) {if (this.Ef && b) {this.ak(-1, true, a, false);} else {this.sp(-1, true, a, false);}})"

I noticed the exact function names might vary with different implementations, so be sure you check yours. It's not hard to imagine the ak method does the smooth zooming and the sp method does the regular zooming. The Ef variable holds a boolean indicating wether smooth zooming is enabled or not. Now let's dig into that ak method. What are its arguments? To be specific: what is the a (the third) argument? Enter the following in the Firebug console:

z = map.ak;
map.ak = function(){console.log(arguments); z.apply(this,arguments);}

Now zoom in or out with your scrollwheel and watch the console window.

That's coordinates, or more specific, the coordinates of the zooming center, which is the place that stays stationary while zooming. Its where the mouse is.

Playing around

Play around with this, you can directly call the ak method from within the console, try for instance:

map.ak(1,true,map.getCenter(),false);

Or zoom in more levels at once:

map.ak(5,true,map.getCenter(),false);

Zoom out around the northeast corner:

map.ak(-5,true,map.getBounds().getNorthEast(),false);

Now try setting the last parameter to true, watch what happens.

Got that? instead of zooming in or out around the given coordinates, it zooms so that the given coordinates end up in the center of the map. Pretty cool huh?

Lastly, there is the second boolean parameter. Setting that one to false only resulted in zooming out to the entire world. No idea what that is about, so just leave it true or tell me what is actually is.

The smoothZoomTo method

Here is some code I wrote to smoothly zoom in such a way that a given point is centered and at the given zoomlevel:

map.smoothZoomTo = function(targetCenter,targetZoom,callBack) {
	var currentZoom = this.getZoom();
	var zoomAmount = targetZoom-currentZoom;
	
	if (!this.Ef) {
		this.setZoom(targetZoom);
		this.setCenter(targetCenter);
		if (callBack) {
			callBack.apply(this);
		}
		return;
	}
	
	if (zoomAmount===0) {
		if (callBack) {
			callBack.apply(this);
		}
	} else {
		this.ak(zoomAmount,true,targetCenter,true);
		if (callBack) {
			var zoomEnd = GEvent.addListener(this,'zoomend',function() {
				callBack.apply(this);
				GEvent.removeListener(zoomEnd);
			});
		}
	}
};

Remember the name of the Ef and ak members may vary!

Additional resources (top 15)

Below is a list of additional resources that might contain extra information about the subject at hand. These are all sites linking to this one (i.e. backtracking).

  1. map.zoomIn() return value - Google 搜索 (3)

comment

Ben (2008-10-07)

A similar affect can be achieved without hacking Google's code. For example, just use map.zoomIn(targetLatLng, centerOnTarget, smoothScroll); First argument is a GLatLng, last two are booleans. Note that you can not specify the zoom level in this way.

Casino Tests (2010-02-18)

I observe the same weird behavior as you if using Firefox for a search for a specific address However the slider still appears on the same searched address using Chrome. This let me assume they GoogleGods made a kind of mistake while playing with their scripts.

diabetes cure (2010-04-14)

Hi there, I have placed a google map into a how to find us section on a webpage I am making, but when I put the <div> element for it in there, the whole web page shifts to the left by a few pixels. I have tried different positioning on it but nothing seems to help. How can I fix this problem?

bad credit car loan (2010-05-25)

I just got a motorola cliq and have seen articles all over advertising the new update to google maps allowing navigation but when i go to google mobile it says to access the android market through my phone and download the latest google maps but when i do that google maps does not appear in the market. Is this becuase i am not on the Droid or is there some other problem that is not allowing me to find this new update?

Add comment
older articles

AdministrationAtom feed