谷歌地圖層行列是以左上角爲原點,瓦片編號是向右和向下,沒有負數。
谷歌是從左上角經緯度(-180,90)度開始計算瓦片,在第0級時谷歌將世界地圖分爲1塊,第1級的時候分爲4塊。
層zoom(z),列col(x),行row(y)
如何在已知層行列情況計算出該瓦片對應的經緯度座標,和已知經緯度情況下計算該點在不同層級下的瓦片位置,以左上角爲原點。
/**
* 谷歌下轉換經緯度對應的層行列
*
* @param lon 經度
* @param lat 維度
* @param zoom 在第zoom層進行轉換
* @return
*/
public static int[] GoogleLonLatToXYZ(double lon, double lat, int zoom) {
double n = Math.pow(2, zoom);
double tileX = ((lon + 180) / 360) * n;
double tileY = (1 - (Math.log(Math.tan(Math.toRadians(lat)) + (1 / Math.cos(Math.toRadians(lat)))) / Math.PI)) / 2 * n;
int[] xy = new int[2];
xy[0] = (int) Math.floor(tileX);
xy[1] = (int) Math.floor(tileY);
return xy;
}
/**
* 層行列轉經緯度
*
* @param x
* @param y
* @param z
* @return
*/
public static double[] XYZtoLonlat(int z, int x, int y) {
double n = Math.pow(2, z);
double lon = x / n * 360.0 - 180.0;
double lat = Math.atan(Math.sinh(Math.PI * (1 - 2 * y / n)));
lat = lat * 180.0 / Math.PI;
double[] lonlat = new double[2];
lonlat[0] = lon;
lonlat[1] = lat;
return lonlat;
}
如果想已左下角爲原點的xyz,只需要將y軸反轉即可:
int ext = (int) Math.pow(2, z);
int changeRow = ext - row - 1;