理论公式:
z: [0-18] x,y: [0-]
在第z级别,x,y方向的瓦片个数均为:
图片(z,x,y)像素(m,n)[注:像素坐标以左上角为原点,x轴向右,y轴向下]的经纬度[单位:度]分别为:
—————————————————————————-
已知经纬度(单位:度),求瓦片编号x,y:
(来源:http://note.youdao.com/publicshare/?id=f4335261b50cb223ebebf05acbb0cd10&type=note#/)
代码函数:
public double calX(double lng, int z) { return Math.floor(Math.pow(2, z – 1) * (lng / 180 + 1)); } public double calY(double lat, int z) { double x = Math.tan(Math.PI * lat / 180); double y = 1 / Math.cos(Math.PI * lat / 180); double q = Math.log(x + y) / Math.PI; return Math.floor(Math.pow(2, z – 1) * (1 – q)); } for (int z = 1; z <= 18; z++) {//已知经纬度(单位:度),求瓦片编号x,y:int x = (int) calX(startLng, z); // 起始xint y = (int) calY(startLat, z); // 起始yint x1 = (int) calX(endLng, z); // 终止xint y1 = (int) calY(endLat, z); // 终止yint t = 0;if (x > x1){ // 如果出现x > x1的情况t = x;x = x1;x1 = t;}if (y > y1){ // 如果出现y > y1的情况t = y;y = y1;y1 = t;}System.out.println(z + ” === ” + x + ” –> ” + x1);System.out.println(z + ” === ” + y + ” –> ” + y1);for (int k = x; k <= x1; k++) {for (int j = y; j <= y1; j++) {try {downLoadFromUrl(“https://a.tile.openstreetmap.org/” + z + “/” + k + “/” + j + “.png”);} catch (IOException e) {e.printStackTrace();}}}}