How Google Map Works(google map是怎样工作的 )

Google Map 怎样工作

这是我对google map工作的一个分析,特别是地图卫片怎样被编码.google map 用简单的url预载入卫片的方式获得。这篇文章解释怎样建立从它们的地理坐标(经纬度)来建立url。

 

地图卫片编码

1) Google Map 用两个不同的算法来编码卫片们的定位。

 

比如说,有这么一个url: http://mt1.google.com/mt?n=404&v=w2.12&x=130&y=93&zoom=9,其中X, Y坐标,和一个比例因素。比例因素从最大的17到最小的0。在zoom=17,整个地球在一个卫片中,在zoom=16,地球被卫成2*2片……以此类推,每一级比例使卫片多分为4片,所以在z比例因素中,水平和垂直的卫片数目为2^(17-z)个.

 

算法:从经纬度和一个比例因素中查找一个卫片坐标

latitude=90-latitude;

longitude=180+longitude;

double latTileSize=180/(pow(2,(17-zoom)));

double longTileSize=360/(pow(2,(17-zoom)));

int tilex=(int)(longitude/longTileSize);

int tiley=(int)(latitude/latTileSize);

事实上,这个算法理论上是覆盖整个地区位置的但并不匹配整个地球。

 

服务:

Google用了4个服务来平衡加载,它们是m1,m2,m3,m4

卫片大小:

每个卫片是一个256*256的png格式图片.

 

2) 对于卫星图片,编码有些不同。

象这样的链接:http://kh0.google.com/kh?n=404&v=8&t=trtqtt 。’t’参数编码图片定位。参数的长度指定一个zoom水平。

要看整个地球,用’t=t’,这给单一的卫片代表地球。在下一个zoom水平,这个卫片北分成4个象限,按顺时针方向: ‘q’ ‘r’ ‘s’ 和 ‘t’。看其中一个象限,增加一个字母在你所能看到的图象上。例如:’t=tq’,给’t’图象的左上象限。

 

算法:从经纬度和一个比例因素中查找一个卫片坐标

 

  
  
1 double xmin =- 180 ;
2
3   double xmax = 180 ;
4
5   double ymin =- 90 ;
6
7   double ymax = 90 ;
8
9   double xmid = 0 ;
10
11   double ymid = 0 ;
12
13
14
15   string location = " t " ;
16
17 double halflat = latitude / 2 ;
18
19 for ( int i = 0 ; i < zoom; i ++ )
20
21 {
22
23 xmoy = (xmax + xmin) / 2 ;
24
25 ymoy = (ymax + ymin) / 2 ;
26
27 if (halflat > ymoy) // upper part (q or r)
28
29 {
30
31 ymin = ymoy;
32
33 if (longitude < xmoy)
34
35 { /* q */
36
37 location += " q " ;
38
39 xmax = xmoy;
40
41 }
42
43 else
44
45 { /* r */
46
47 location += " r " ;
48
49 xmin = xmoy;
50
51 }
52
53 }
54
55 else // lower part (t or s)
56
57 {
58
59 ymax = ymoy;
60
61 if (longitude < xmoy)
62
63 { /* t */
64
65 location += " t " ;
66
67 xmax = xmoy;
68
69 }
70
71 else
72
73 { /* s */
74
75 location += " s " ;
76
77 xmin = xmoy;
78
79 }
80
81 }
82
83 }
84
85

 

 

和前面一样,这个算法理论上是覆盖整个地区位置的但并不匹配整个地球。

服务:

Google用了4个服务来平衡加载,它们是kh0, kh1, kh2 and kh3

卫片大小:

每个卫片是一个256*256的jpg格式图片

 

例子:

http://kh0.google.com/kh?n=404&v=8&t=t

 

并且四个象限加载 :

  • 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

 

 

 

http://www.cnblogs.com/liping13599168/archive/2007/02/23/654002.html

Shenzhen Universiade - Shenzhen 2011 Summer Universiade

 

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

Collapse Copy Code
//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 360longitude=180+longitude;
//find tile size from zoom leveldouble latTileSize=180/(pow(2,(17-zoom)));
double longTileSize=360/(pow(2,(17-zoom)));
//find the tile coordinatesint 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

Collapse Copy Code
//initialise the variables;

 

  
  
1 double xmin =- 180 ;
2 double xmax = 180 ;
3 double ymin =- 90 ;
4 double ymax = 90 ;
5 double xmid = 0 ;
6 double ymid = 0 ;
7 string location = " t " ;
8 // Google uses a latitude divided by 2;
9 double halflat = latitude / 2 ;
10 for ( int i = 0 ; i < zoom; i ++ )
11 {
12 xmoy = (xmax + xmin) / 2 ;
13 ymoy = (ymax + ymin) / 2 ;
14 if (halflat > ymoy) // upper part (q or r)
15 {
16 ymin = ymoy;
17 if (longitude < xmoy)
18 {
19 /* q */
20 location += " q " ;
21 xmax = xmoy;
22 } else
23 {
24 /* r */
25 location += " r " ;
26 xmin = xmoy;
27 }
28 }
29 else // lower part (t or s)
30 {
31 ymax = ymoy;
32 if (longitude < xmoy)
33 {
34 /* t */
35 location += " t " ;
36 xmax = xmoy;
37 }
38 else
39 { /* s */
40 location += " s " ;
41 xmin = xmoy;
42 }
43 }
44 } // here, the location should contain the string corresponding to the tile...
45

 

 

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.

Collapse Copy Code
/**<summary>Get the vertical tile number from a latitudeusing 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)

About the Author

Pascal Buirey


Member

Occupation: Web Developer
Location: France France

 

Other popular Scrapbook articles:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值