揭密Google Map的工作原理(轉)


 

  • 板塊:技術
  • 作者:文/ Pascal Buirey 譯/Amanda
  • 編者按:我分析了google map的工作原理,特別是如何進行tiles編碼。Google map使用預着色tiles,可以通過一個簡單的url地址實現。
  • 作者簡介:
  • 來源:

  我分析了google map的工作原理,特別是如何進行tiles編碼。Google map使用預着色tiles,可以通過一個簡單的url地址實現。這篇文章將解釋如何從地理坐標(經緯度)建立tile的url地址。


Map tile編碼

  Google map使用兩種算法對tile的位置進行編碼。

  對於Google map,tile的url地址類似於:http://mt1.google.com/mt?n=404&v=w2.89&x=130&y=93&zoom=9使用x和y來設置tile坐標和放大因子。放大因子從17(完全縮小)到0(最大比例)。當放大因子為17時,整個地球在一個tile中顯示,此時x=0 ,y=0;放大因子為16時,地球被分為2x2部分,這時0<=x<=1 且0<=y<=1。每放大一次,每個tile被分為4個部分。因此,當放大因子為z時,顯示的水平和垂直tile個數為2^(17-z)。


算法:尋找經緯度和放大因子


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


  事實上,這個算法只是理論上的,覆蓋的地帶還無法和整個地球匹配。


  服務器:
  Google使用4台服務器維持加載。分別是mt0, mt1, mt2 and mt3。


  顯示位置:
  每個tile為256x256 的.png格式的圖像。


  對於衛星影像,編碼方式有點不同


  Tile的url地址類似於http://kh0.google.com/kh?n=404&v=8&t=trtqtthttp://kh.google.com/kh?v=34&t=trtqtt其中參數t對影像的位置進行編碼。參數的長度表示縮放級別。


  t=t時,可觀察整個地球,只有一個tile表示地球。下一個放大級別是,該tile被分為4個象限,從左上順時針開始分別是'q' 'r' 's' 和 't'。在你觀察的影像那個象限后添加字母,就可以看見下一級象限。比如,'t=tq'時,給出't' 影像的左上象限。依次類推,可以表示每個縮放級別…


算法:尋找經緯度和放大因子


 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 the string corresponding to the tile...


  同樣,這個算法也是理論上的,覆蓋地帶還無法和整個地球匹配。

  服務器:
  Google使用4台服務器維持加載。分別是kh0, kh1, kh2 and kh3。

  顯示位置:
  每個tile為256x256 的.jpg格式圖像。

  墨卡托投影

  由於在顯示時使用了墨卡托投影,因此上述的算法需要進行修改。在墨卡托投影中,兩條緯線間的距離不一定相等,所以描述tile的角度依據它的垂直位置。


以下代碼為從tile的緯度位置計算的tile的垂直個數

 Collapse
/**<summary>Get the vertical tile number from a latitude using mercator ptrojection 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);
        }


  覆蓋地帶:
  理論上,緯度范圍應該是-90度到90度,但事實上,由於墨卡托投影使得兩級無窮大,覆蓋的地帶小於-90 到 90。實際最大的緯度可通過公式Y = 1/2((1+sin(lat))/(1-sin(lat)))求得,其中PI =3.1415926。


  保護:
  Google map使用保護機制維持高質量服務。如果某個用戶要求太多,Google map將添加它的IP地址到黑名單,並發送一條有意思的消息:

  Google Error


  我們很抱歉... ...您的查詢像病毒或黑客應用程序的自動化請求。為了保護我們的用戶,現在無法處理您的請求。我們將盡快恢復您的訪問,所以請重新試試。同時,如果您懷疑您的計算機或網絡被感染了,可以清除病毒。為給您帶來的不便表示歉意,希望能在Google上再一次見到您。

  為了避免被列入黑名單,開發員應該使用隱藏機制…

  很有趣吧?

  這里有個用C#寫的簡單代碼:Download GoogleMapSample.zip - 40.4 KB
注:Google map 改變了地圖的參數v,當我寫這篇文章時,v為2.12,但現在變為2.43。我覺得這個有點像版本號或其它什么的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM