Revealing the workings of Google Map

Source: Internet
Author: User
Tags pow sin

article Information Plate : Technical author : text/Pascal Buirey translation/amanda editor 's note: I analyzed how Google map works, especially how to tiles code. Google Map uses a tiles, which can be implemented via a simple URL address. Author Introduction : source :

I analyzed how Google map works, especially how to tiles code. Google Map uses a tiles, which can be implemented via a simple URL address. This article will explain how to create tile URL addresses from geographical coordinates (latitude and longitude).


Map Tile Encoding

Google Map uses two algorithms to encode the location of the tile.

The URL address for Google Map,tile is similar to the following: http://mt1.google.com/mt?n=404&v=w2.12&x=130&y=93&zoom= 9 use X and Y to set the tile coordinates and amplification factor. The magnification factor is from 17 (completely reduced) to 0 (the maximum proportion). When the amplification factor is 17 o'clock, the whole earth is shown in a tile, at this time x=0, y=0, amplification factor is 16 o'clock, the earth is divided into the 2x2 part, then 0<=x<=1 and 0<=y<=1. Each tile is divided into 4 parts each time it is amplified. Therefore, when the magnification factor is Z, the number of horizontal and vertical tile displayed is 2^ (17-z).


algorithm: Looking for latitude and longitude and amplification factor


Correct the latitude to go from 0 (north) to 180 (south),
instead (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 only theoretically, the covered zone can not match the entire Earth.


Server:
Google uses 4 servers to maintain load. respectively, Mt0, MT1, Mt2 and Mt3.


Show Location:
Each tile is an image in the. png format of 256x256.


For satellite imagery, the coding is a little different.


Tile's URL address is similar to HTTP://KH0.GOOGLE.COM/KH?N=404&V=8&T=TRTQTT, where the parameter T encodes the location of the image. The length of the parameter represents the zoom level.


When t=t, you can observe the whole Earth, only one tile represents the Earth. The next magnification level is that the tile is divided into 4 quadrants, starting with the ' Q ' r ' s ' and ' t ' from the upper left clockwise respectively. You can see the next quadrant by adding letters to the quadrant after the image you are looking at. For example, ' T=TQ ' gives the upper left quadrant of the ' t ' image. By analogy, you can represent each zoom level ...


algorithm: Looking for latitude and longitude and amplification factor


Collapse
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 use 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 contains of the string corresponding to the tile ...


Again, the algorithm is theoretically, and the covering zone cannot match the entire Earth.

Server:
Google uses 4 servers to maintain load. respectively, Kh0, Kh1, Kh2 and Kh3.

Show Location:
Each tile is an 256x256. jpg format image.

Mercator projection

Because the Mercator projection is used in the display, the above algorithm needs to be modified. In the Mercator projection, the distance between two parallel lines is not necessarily equal, so the angle of the description tile is based on its vertical position.


The following code is the vertical number of tile computed from the latitude position of the tile

Collapse
/**<summary>get the vertical tile number from a latitude using Mercator ptrojection
private int getmercatorlatitude (double lati)
{
Double Maxlat = Math.PI;

Double lat = lati;

if (Lat >) 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);
}


Cover Zone:
Theoretically, the latitude range should be-90 degrees to 90 degrees, but in fact, because the Mercator projection makes the two class infinity, the covered strip is less than-90 to 90. The actual maximum latitude can be obtained by means of the formula Y = 1/2 (1+sin (LAT))/(1-sin (LAT)), where pi = 3.1415926.


Protection:
Google maps uses the protection mechanism to maintain high quality services. If a user asks for too much, Google map will add its IP address to the blacklist and send an interesting message:

Google Error


We are sorry ... Your query is like an automated request for a virus or hacker application. To protect our users, your request cannot be processed at this time. We will resume your visit as soon as possible, so please try again. Also, if you suspect your computer or network is infected, you can clear the virus. Sorry for the inconvenience, I hope to see you again on Google.

To avoid being blacklisted, developers should use the hidden mechanism ...

It's funny.

Here's a simple code written in C #: Download googlemapsample.zip-40.4 KB
Note: Google maps changed the parameters of the map V, when I wrote this article, V is 2.12, but now it is 2.43. I think it's a bit like the version number or something.

Complete content please download "3S Newsweek" PDF version read.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.