Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

How Google Map Works

Rate me:
Please Sign up or sign in to vote.
4.86/5 (82 votes)
7 Jan 2008CPOL4 min read 836.7K   4.6K   204   202
An analysis of the encoding of the tiles used by Google map

Introduction

This is my analysis about how Google map works, and specially how the tiles are encoded. Google map uses pre-rendered tiles that can be obtained with a simple URL. This article explains how to build the URL for a tile from its geo coordinates (latitude/longitude).

Map Tile Encoding

Google map uses two different algorithms to encode the location of the tiles.

For Google map, the URL of a tile looks like http://mt1.google.com/mt?n=404&v=w2.12&x=130&y=93&zoom=9 using x and y for the tile coordinates, and a zoom factor. The zoom factor goes from 17 (fully zoomed out) to 0 (maximum definition). At a factor 17, the whole earth is in one tile where x=0 and y=0. At a factor 16, the earth is divided in 2x2 parts, where 0<=x<=1 and 0<=y<=1, and at each zoom step, each tile is divided into 4 parts. So at a zoom factor Z, the number of horizontal and vertical tiles is 2^(17-z)

Algorithm to Find a Tile from a Latitude, a Longitude and a Zoom Factor

C#
//correct the latitude to go from 0 (north) to 180 (south),
// instead of 90(north) to -90(south)
latitude=90-latitude;

//correct the longitude to go from 0 to 360
longitude=180+longitude;

//find tile size from zoom level
double latTileSize=180/(pow(2,(17-zoom)));
double longTileSize=360/(pow(2,(17-zoom)));

//find the tile coordinates
int tilex=(int)(longitude/longTileSize);
int tiley=(int)(latitude/latTileSize);

In fact this algorithm is theoretical as the covered zone doesn't match the whole globe.

Servers

Google uses four servers to balance the load. These are mt0, mt1, mt2 and mt3.

Tile Size

Each tile is a 256x256 PNG picture.

For Satellite Images, the Encoding is a Bit Different

The URL looks like http://kh0.google.com/kh?n=404&v=8&t=trtqtt where the 't' parameters encode the image location. The length of the parameter indicates a zoom level.

To see the whole globe, just use 't=t'. This gives a single tile representing the earth. For the next zoom level, this tile is divided into 4 quadrants, called, clockwise from top left : 'q' 'r' 's' and 't'. To see a quadrant, just append the letter of that quadrant to the image you are viewing. For example :'t=tq' will give the upper left quadrant of the 't' image. And so on at each zoom level...

Algorithm to Find a Tile from a Latitude, a Longitude and a Zoom Factor

C#
//initialise the variables;
double xmin=-180;
double xmax=180;
double ymin=-90;
double ymax=90;

double xmid=0;
double ymid=0;

string location="t";

//Google uses a latitude divided by 2;
double halflat = latitude / 2;

for (int i = 0; i < zoom; i++)
    {
        xmoy = (xmax + xmin) / 2;
        ymoy = (ymax + ymin) / 2;
        if (halflat > ymoy) //upper part (q or r)
            {
            ymin = ymoy;
            if (longitude < xmoy)
            { /*q*/
                location+= "q";
                xmax = xmoy;
            }
            else
            {/*r*/
                location+= "r";
                xmin = xmoy;
            }
        }
        else //lower part (t or s)
        {
            ymax = ymoy;
            if (longitude < xmoy)
            { /*t*/

                location+= "t";
                xmax = xmoy;
            }
            else
            {/*s*/
                location+= "s";
                xmin = xmoy;
            }
        }
    }
//here, the location should contain the string corresponding to the tile...

Again, this algorithm is quite theoretical, as the covered zone doesn't match the full globe.

Servers

Google uses four servers to balance the load. These are kh0, kh1, kh2 and kh3.

Tile Size

Each tile is a 256x256 JPG picture.

Mercator Projection

Due to the Mercator projection, the above algorithm has to be modified. In Mercator projection, the spacing between two parallels is not constant. So the angle described by a tile depends on its vertical position.

Here comes a piece of code to compute a tile's vertical number from its latitude.

C#
/**<summary>Get the vertical tile number from a latitude
using Mercator projection formula</summary>*/
        private int getMercatorLatitude(double lati)
        {
            double maxlat = Math.PI;

            double lat = lati;

            if (lat > 90) lat = lat - 180;
            if (lat < -90) lat = lat + 180;

            // conversion degre=>radians
            double phi = Math.PI * lat / 180;

            double res;
            //double temp = Math.Tan(Math.PI / 4 - phi / 2);
            //res = Math.Log(temp);
            res = 0.5 * Math.Log((1 + Math.Sin(phi)) / (1 - Math.Sin(phi)));
            double maxTileY = Math.Pow(2, zoom);
            int result = (int)(((1 - res / maxlat) / 2) * (maxTileY));

            return (result);
        }

Covered Zone

Theoretically, latitude should go from -90 to 90, but in fact due to the Mercator projection which sends the poles to the infinites, the covered zone is a bit less than -90 to 90. In fact the maximum latitude is the one that gives PI (3.1415926) on the Mercator projection, using the formula Y = 1/2((1+sin(lat))/(1-sin(lat))) (see the link in the Mercator paragraph).

Protection

Google map uses a protection mechanism to keep a good quality of service. If one makes too many requests, Google map will add its IP address to a blacklist, and send a nice message:

Google Error

We're sorry... ... but your query looks similar to automated requests from a computer virus or spyware application. To protect our users, we can't process your request right now. We'll restore your access as quickly as possible, so try again soon. In the meantime, if you suspect that your computer or network has been infected, you might want to run a virus checker or spyware remover to make sure that your systems are free of viruses and other spurious software. We apologize for the inconvenience, and hope we'll see you again on Google.

To avoid being blacklisted, developers should use a caching mechanism if possible...

Sat Examples

See the whole globe at http://kh0.google.com/kh?n=404&v=8&t=t.

And the four corresponding quadrants: (note the 4 servers name to balance the load)

  • http://kh0.google.com/kh?n=404&v=8&t=tq
  • http://kh1.google.com/kh?n=404&v=8&t=tr
  • http://kh2.google.com/kh?n=404&v=8&t=ts
  • http://kh3.google.com/kh?n=404&v=8&t=tt

Map Examples

See the whole globe at http://mt1.google.com/mt?n=404&v=&x=0&y=0&zoom=17.

And the four corresponding quadrants:

  • http://mt0.google.com/mt?n=404&v=&x=0&y=0&zoom=16
  • http://mt1.google.com/mt?n=404&v=&x=1&y=0&zoom=16
  • http://mt2.google.com/mt?n=404&v=&x=0&y=1&zoom=16
  • http://mt3.google.com/mt?n=404&v=&x=1&y=1&zoom=16

Nice, isn't it?

For a sample code written in C#, see the download at the top of this article.

History

  • Article edited: Google map has changed the v parameter for the maps. It was 2.12 when I wrote this article, but it's now 2.66. I suppose this is a version number or something like that...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to mark a point on given tile? Pin
pelsta8-May-07 1:10
pelsta8-May-07 1:10 
AnswerRe: How to mark a point on given tile? Pin
MrJoe018-May-07 1:42
MrJoe018-May-07 1:42 
GeneralRe: How to mark a point on given tile? Pin
pelsta8-May-07 4:07
pelsta8-May-07 4:07 
AnswerRe: How to mark a point on given tile? Pin
MissUEveryDay28-May-07 18:33
MissUEveryDay28-May-07 18:33 
AnswerRe: How to mark a point on given tile? Pin
pelsta29-May-07 10:10
pelsta29-May-07 10:10 
Questionlatitude problem Pin
joyce97313-Mar-07 4:11
joyce97313-Mar-07 4:11 
AnswerRe: latitude problem Pin
Pascal Buirey18-Mar-07 22:32
Pascal Buirey18-Mar-07 22:32 
GeneralRe: latitude problem Pin
joyce97318-Mar-07 23:33
joyce97318-Mar-07 23:33 
GeneralRe: latitude problem Pin
Pascal Buirey19-Mar-07 21:39
Pascal Buirey19-Mar-07 21:39 
GeneralRe: latitude problem Pin
joyce97319-Mar-07 22:31
joyce97319-Mar-07 22:31 
GeneralRe: latitude problem Pin
Pascal Buirey22-Mar-07 7:05
Pascal Buirey22-Mar-07 7:05 
GeneralRe: latitude problem Pin
joyce97322-Mar-07 9:50
joyce97322-Mar-07 9:50 
GeneralMinor Modifications Pin
e_akhras24-Mar-07 6:09
e_akhras24-Mar-07 6:09 
GeneralRe: Minor Modifications Pin
Pascal Buirey25-Mar-07 21:01
Pascal Buirey25-Mar-07 21:01 
GeneralRe: Minor Modifications Pin
e_akhras26-Mar-07 5:02
e_akhras26-Mar-07 5:02 
GeneralRe: Minor Modifications Pin
Mark Johnson10-May-07 17:28
Mark Johnson10-May-07 17:28 
QuestionCan't get more than 13 Zoom Levels ! Pin
e_akhras11-Mar-07 4:05
e_akhras11-Mar-07 4:05 
AnswerRe: Can't get more than 13 Zoom Levels ! Pin
Pascal Buirey18-Mar-07 22:46
Pascal Buirey18-Mar-07 22:46 
GeneralRe: Can't get more than 13 Zoom Levels ! Pin
e_akhras19-Mar-07 0:09
e_akhras19-Mar-07 0:09 
QuestionLatitude for tiles Pin
heiser5-Nov-06 9:48
heiser5-Nov-06 9:48 
AnswerRe: Latitude for tiles Pin
Pascal Buirey18-Mar-07 22:27
Pascal Buirey18-Mar-07 22:27 
GeneralZooming Issues Pin
mr gen20-Oct-06 21:34
mr gen20-Oct-06 21:34 
GeneralRe: Zooming Issues Pin
Pascal Buirey23-Oct-06 1:26
Pascal Buirey23-Oct-06 1:26 
Question[Message Deleted] [modified] Pin
GIS-guy1-Oct-06 7:08
GIS-guy1-Oct-06 7:08 
AnswerRe: The coordinate of the tile corners? Pin
Pascal Buirey23-Oct-06 1:22
Pascal Buirey23-Oct-06 1:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.