由經緯度坐標得到騰訊地圖的瓦片/切片行列號


參考:https://blog.csdn.net/mygisforum/article/details/22997879

   https://blog.csdn.net/u013929284/article/details/53614281

   https://blog.csdn.net/shaxiaozilove/article/details/54908569

項目需要根據數據的瓦片行列號建立文件目錄,而已知的位置信息是經緯度,下面是解決方法:

一、經緯度坐標 => 投影坐標;騰訊地圖使用的是Web Mercator投影。

    private double[] getXY(double lon, double lat) {
        double earthRad = 6378137.0;
        double x = lon * Math.PI / 180.0 * earthRad;
        double a = lat * Math.PI / 180.0;
        double y = earthRad / 2.0 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
        return new double[]{x, y};
    }

二、騰訊地圖坐標原點在左下角,根據瓦片級數求出瓦片長寬;18級代表該級數下,每行每列的瓦片數為Math.pow(2,18)。

三、計算行列號,即投影坐標值之差 / 瓦片大小;下為完整代碼,其中-85.05112877980659, -180是原點的經緯度。

        double[] sz = getXY(120.141554 ,30.273926);
        int z = 18;
        double[] bl = getXY(-180, -85.05112877980659);
        double[] tl = getXY(-180, 85.05112877980659);
        double[] br = getXY(180, -85.05112877980659);
        System.out.println(String.format("坐標BL點:%f,%f", bl[0], bl[1]));
        System.out.println(String.format("坐標TL點:%f,%f", tl[0], tl[1]));
        System.out.println(String.format("坐標BR點:%f,%f", br[0], br[1]));
        System.out.println(String.format("深圳坐標:%f,%f", sz[0], sz[1]));
        double w = (br[0] - bl[0]) / Math.pow(2, z);//格網寬度
        double h = (tl[1] - bl[1]) / Math.pow(2, z);//格網高度
        System.out.println(String.format("格網大小:%f x %f", w, h));
        int[] gridxy = new int[2];
        int c = (int) ((sz[0] - bl[0]) / w);
        int r = (int) ((sz[1] - bl[1]) / h);
        System.out.println(String.format("對應行列號:%d,%d", c, r));
        double c_d = Math.floor(c / 16.0);
        double r_d = Math.floor(r / 16.0);
        System.out.println(String.format("對應請求url:http://p3.map.gtimg.com/maptilesv2/%d/%d/%d/%d_%d.png", z, (int) c_d, (int) r_d, c, r));

最后的url可直接訪問,測試坐標為浙江大學西溪校區主教學樓門口的圓盤。


免責聲明!

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



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