1、左上角为原点
/**
* 谷歌下转换经纬度对应的层行列
* @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;
}
function XYZToLonLat (z, x, y) {
const n = Math.pow(2, z)
const lon = x / n * 360.0 - 180.0
let lat = Math.atan(Math.sinh(Math.PI * (1 - 2 * y / n)))
lat = lat * 180.0 / Math.PI
return [lon, lat]
}
转载自https://blog.csdn.net/qq_18298439/article/details/93219931
2、根据层行列获取边界以及中心点坐标
function tile2long(x,z) {
return (x/Math.pow(2,z)*360-180)
}
function tile2lat(y,z) {
const n=Math.PI-2*Math.PI*y/Math.pow(2,z)
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))))
}
function result (x, y, z) {
const SW_long = tile2long(x, z)
const SW_lat = tile2lat(y+1, z)
const NE_long = tile2long(x+1, z)
const NE_lat = tile2lat(y,z)
const export_bbox = SW_long + ',' + SW_lat + ',' + NE_long + ',' + NE_lat
const center = [(NE_long - SW_long) / 2 + SW_long, (NE_lat - SW_lat) / 2 + SW_lat]
console.log(export_bbox, center)
}