Google maps hacking

After I saw Google Maps Hacking and Bookmarklets it was hard to resist making the route following hacks even better. The version below follows routes smoother and handles very long routes (e.g. accross the country) a lot better. Credits are due "Follower" for the original idea.

February 13, 2005: Original version.
February 14, 2005: Smoother now.
March 17, 2005: Updated to work with Google Maps 5.
March 19, 2005: Added variable declarations to avoid variable name clashes.
May 20, 2005: Finally got around to removing Google Maps version dependency.


Google maps smooth route bookmarklet
javascript:
(function ()
{
var p, c1, c2, i;
	if (!_m.map.me)
	{
		_m.map.me = _m.map.createLocationMarker('http://libgmail.sourceforge.net/man.png', _IconClass.get('local'));
	} 
	_m.map.timeSlice = 60;
	_m.map.totalTime = 3000;
	_m.map.sliceCount = _m.map.totalTime/_m.map.timeSlice;
	_m.map.totalDistance = 0;
	_m.map.c3 = new Object();
	p = _m.map.directions.polyline;
		
	_m.map.cumDistance = new Array(p.numPoints);
	_m.map.cumDistance[0] = 0;
	c1 = p.getPoint(0);
	for (i=1; i<p.numPoints; i++)
	{
		c2 = p.getPoint(i);
		_m.map.cumDistance[i] = _m.map.cumDistance[i-1]+ Math.sqrt(Math.pow(c2.x-c1.x, 2)+Math.pow(c2.y-c1.y, 2));
		c1 = c2;
	}
	_m.map.speed = _m.map.cumDistance[p.numPoints-1]/_m.map.sliceCount;

	mv = function(i) 
		{ 
		var p, j, d, l;
			p = _m.map.directions.polyline;
			j = 0;
			while(_m.map.cumDistance[j+1] < (i*_m.map.speed))
			{
				j++;
			}
			c1 = p.getPoint(j);
			c2 = p.getPoint(j+1);
			d = _m.map.cumDistance[j+1] - _m.map.cumDistance[j];
			if(d)
			{
				l = (i*_m.map.speed)-_m.map.cumDistance[j];
				_m.map.c3.x = c2.x - (c2.x-c1.x)*(d-l)/d;
				_m.map.c3.y = c2.y - (c2.y-c1.y)*(d-l)/d;
			}
			else
			{
				_m.map.c3.x = c2.x;
				_m.map.c3.y = c2.y;
			}
			
			_m.map.recenterOrPanToLatLng(_m.map.c3);
			_m.map.setMarkerPosition(_m.map.me, _IconClass.get('local'), _m.map.c3);
			if (i < _m.map.sliceCount) 
			{
				window.setTimeout('mv('+(i+1) + ')',_m.map.timeSlice);
			} 
			else 
			{
				_m.map.me.hide();
			}
		};

	_m.map.me.show();
	mv(1);
}
)();





Note that this does not work in IE6 due to its 508 character limit on links. Should work in IE5, Firefox, etc.

More improvements to come...

Ayhan Ergul
email.py
'@'.join(['ergul', '.'.join(['acm','org'])])


The contents of this page may be copied with attribution or link back to here.