Click here to Skip to main content
11,567,143 members (50,370 online)
Click here to Skip to main content

How Google Map Works

, 7 Jan 2008 CPOL 568.7K 3.8K 202
Rate this:
Please Sign up or sign in to vote.
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

//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

//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.

/**<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)

Share

About the Author

Pascal Buirey
Web Developer
France France
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questiondownloading google map tiles -help Pin
anuja.rawat81022-Feb-12 17:50
memberanuja.rawat81022-Feb-12 17:50 
QuestionDo you know the tile format of 45 degreee images? Pin
netying24-Sep-10 15:46
membernetying24-Sep-10 15:46 
GeneralGetting satellite tile image Pin
Aslan747029-Jun-10 23:12
memberAslan747029-Jun-10 23:12 
GeneralGoogle's server on error Pin
borowiak27-Sep-09 21:53
memberborowiak27-Sep-09 21:53 
GeneralPlease Pin
mbaocha3-May-09 22:06
membermbaocha3-May-09 22:06 
GeneralRe: Please Pin
radioman.lt19-Jun-09 11:57
memberradioman.lt19-Jun-09 11:57 
Generalkh0 at v34 Pin
eyad al akhras27-Feb-09 1:41
membereyad al akhras27-Feb-09 1:41 
GeneralRe: kh0 at v34 Pin
vincegavin1-Mar-09 5:08
membervincegavin1-Mar-09 5:08 
GeneralRe: kh0 at v34 Pin
eyad al akhras1-Mar-09 19:41
membereyad al akhras1-Mar-09 19:41 
GeneralRe: kh0 at v34 Pin
vincegavin22-Mar-09 0:19
membervincegavin22-Mar-09 0:19 
Thank you for answering my question.

I want to know whether there are some methods that can help me to know the value of parameter "v" or not. Can you telp me the method ? please. Also can you tell me the meaning of the parameter "n"? and How I can find the value of the parameter "n"?

Thank you !
GeneralRe: kh0 at v34 Pin
eyad al akhras22-Mar-09 1:36
membereyad al akhras22-Mar-09 1:36 
GeneralRe: kh0 at v34 Pin
vincegavin22-Mar-09 2:44
membervincegavin22-Mar-09 2:44 
Questionwhat 's Current Satellitemap-servers name? Pin
dip2k1-Feb-09 18:33
memberdip2k1-Feb-09 18:33 
AnswerRe: what 's Current Satellitemap-servers name? Pin
eyad al akhras22-Mar-09 1:38
membereyad al akhras22-Mar-09 1:38 
General.net2 control Pin
radioman.lt6-Dec-08 9:06
memberradioman.lt6-Dec-08 9:06 
General[Message Removed] Pin
Katekortez25-Oct-08 9:29
memberKatekortez25-Oct-08 9:29 
QuestionSatellitemap-servers down? Pin
segu@gmx.net27-Jul-08 19:21
membersegu@gmx.net27-Jul-08 19:21 
AnswerRe: Satellitemap-servers down? Pin
CoderCZ12-Aug-08 22:36
memberCoderCZ12-Aug-08 22:36 
GeneralRe: Satellitemap-servers down? Pin
segu@gmx.net13-Aug-08 4:44
membersegu@gmx.net13-Aug-08 4:44 
GeneralRe: Satellitemap-servers down? Pin
CoderCZ13-Aug-08 21:53
memberCoderCZ13-Aug-08 21:53 
GeneralRe: Satellitemap-servers down? Pin
segu@gmx.net13-Aug-08 22:13
membersegu@gmx.net13-Aug-08 22:13 
Generallatitude problem Pin
hiuyil11-Jul-08 7:43
memberhiuyil11-Jul-08 7:43 
GeneralRe: latitude problem Pin
Romain Vallet27-Aug-08 2:03
memberRomain Vallet27-Aug-08 2:03 
GeneralServer numbering and secure word (Galileo) [modified] Pin
CoderCZ24-Jun-08 3:03
memberCoderCZ24-Jun-08 3:03 
GeneralRe: Server numbering and secure word (Galileo) Pin
JasonDiplomat27-Aug-08 13:18
memberJasonDiplomat27-Aug-08 13:18 
GeneralRe: Server numbering and secure word (Galileo) Pin
CoderCZ28-Aug-08 0:42
memberCoderCZ28-Aug-08 0:42 
GeneralRe: Server numbering and secure word (Galileo) Pin
segu@gmx.net27-Aug-08 18:42
membersegu@gmx.net27-Aug-08 18:42 
GeneralRe: Server numbering and secure word (Galileo) Pin
CoderCZ28-Aug-08 0:47
memberCoderCZ28-Aug-08 0:47 
GeneralRe: Server numbering and secure word (Galileo) Pin
segu@gmx.net28-Aug-08 1:01
membersegu@gmx.net28-Aug-08 1:01 
NewsGetting a tile Pin
Member 15975873-Jun-08 1:18
memberMember 15975873-Jun-08 1:18 
GeneralLicence problems Pin
Aleksey Zaharov6-Apr-08 5:11
memberAleksey Zaharov6-Apr-08 5:11 
GeneralRe: Licence problems Pin
Pascal Buirey6-Apr-08 22:12
memberPascal Buirey6-Apr-08 22:12 
GeneralRe: Licence problems Pin
Daniel Ch. Bloch (MCSD, MCAD, MCTS)19-Nov-08 19:27
memberDaniel Ch. Bloch (MCSD, MCAD, MCTS)19-Nov-08 19:27 
GeneralSummary Article [modified] Pin
James_Lin28-Feb-08 13:53
memberJames_Lin28-Feb-08 13:53 
QuestionIs this project dead? Pin
James_Lin20-Feb-08 15:30
memberJames_Lin20-Feb-08 15:30 
AnswerRe: Is this project dead? Pin
Pascal Buirey20-Feb-08 20:47
memberPascal Buirey20-Feb-08 20:47 
GeneralRe: Is this project dead? Pin
James_Lin23-Feb-08 19:07
memberJames_Lin23-Feb-08 19:07 
GeneralRe: Is this project dead? Pin
Pascal Buirey24-Feb-08 21:52
memberPascal Buirey24-Feb-08 21:52 
GeneralRe: Is this project dead? Pin
TimmyTee26-Feb-08 2:51
memberTimmyTee26-Feb-08 2:51 
GeneralRe: Is this project dead? Pin
James_Lin28-Feb-08 9:39
memberJames_Lin28-Feb-08 9:39 
GeneralRe: Is this project dead? Pin
senthilmuruganbtech12-Aug-08 23:24
membersenthilmuruganbtech12-Aug-08 23:24 
AnswerRe: Is this project dead? Pin
senthilmuruganbtech12-Aug-08 23:23
membersenthilmuruganbtech12-Aug-08 23:23 
AnswerRe: Is this project dead? Pin
senthilmuruganbtech12-Aug-08 23:24
membersenthilmuruganbtech12-Aug-08 23:24 
AnswerRe: Is this project dead? Pin
senthilmuruganbtech13-Aug-08 2:11
membersenthilmuruganbtech13-Aug-08 2:11 
Questionhi. how to use real-time tiles...? Pin
hyohaeng31-Jan-08 19:05
memberhyohaeng31-Jan-08 19:05 
GeneralRe: hi. how to use real-time tiles...? Pin
Pascal Buirey31-Jan-08 22:21
memberPascal Buirey31-Jan-08 22:21 
Generalget x,y pixel inside a tile from given GPS coordinates Pin
James_Lin31-Jan-08 10:30
memberJames_Lin31-Jan-08 10:30 
GeneralRe: get x,y pixel inside a tile from given GPS coordinates Pin
Pascal Buirey31-Jan-08 21:54
memberPascal Buirey31-Jan-08 21:54 
GeneralRe: get x,y pixel inside a tile from given GPS coordinates Pin
James_Lin3-Feb-08 9:33
memberJames_Lin3-Feb-08 9:33 
GeneralRe: get x,y pixel inside a tile from given GPS coordinates Pin
Pascal Buirey4-Feb-08 6:07
memberPascal Buirey4-Feb-08 6:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 7 Jan 2008
Article Copyright 2006 by Pascal Buirey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid