This function in javascript returns the X and Y used to fetch google map tiles by giving the longitude and latitude.
function getXYfromLatLon(lat, lon, zoomLevel) {
var lon = 180.0 + lon;
var x = Math.floor( (lon / 360.0) * 131072 ); /* 2^17 = 131072 */
x >>= zoomLevel;
var lat = lat / 180.0 * 3.1415926
var y = Math.PI - 0.5 * Math.log((1+Math.sin(lat))/(1-Math.sin(lat)))
y = Math.floor( (y / 2 / Math.PI) * 131072 );
y >>= zoomLevel;
return new Array(x,y);
}
This function returns the string used to fetch the satellite image tiles by give the X and Y from the getXYfromLatLon().
function getImageTileStrFromXY(x, y, zoomLevel) {
var quarter2letter = new Array("q","r","t","s");
var rtnStr = "t"
for (i=16-zoomLevel; i>=0; i--) {
var quarter =
(x & (1 < < i)) >> i | ( ((y & (1 < < i)) >> i) < < 1 );
rtnStr += quarter2letter[quarter];
}
return rtnStr;
}
Simple usage:
<script language="javascript">
var lon = -122;
var lat = 37;
var zl =3;
var XY = getXYfromLatLon(lat, lon, zl);
document.getElementById('img1').src=
"http://mt0.google.com/mt?v=w2.61&x="+XY[0]+"&y="+XY[1]+"&zoom="+zl;
var tileStr = getImageTileStrFromXY(XY[0], XY[1], zl);
document.getElementById('img2').src=
"http://kh0.google.com/kh?&v=20&t="+tileStr;
</script>