WGS84經緯度 與 web 墨卡托相互轉化 工具類


package ss;
/**
 * gis 地圖展示工具類
 * 
 * @author zhangss
 * @version 1.0 2017-3-21 15:04:21
 * */
public class GisUtil {
    private final static double M_PI = Math.PI;
    
    /**
     * WGS84經緯度轉web Mercator
     * 
     * @param double lon 經度
     * @param double lat 緯度
     * 
     * @return double[] 
     * */
    public static double[] lonLat2Mercator(double lon, double lat){
        double[] xy = new double[2];
        double x = lon *20037508.342789/180;
        double y = Math.log(Math.tan((90+lat)*M_PI/360))/(M_PI/180);
        y = y *20037508.34789/180;
        xy[0] = x;
        xy[1] = y;
        return xy;
    }
    
    /**
     * web Mercator轉WGS84經緯度
     * 
     * @param double mercatorX
     * @param double mercatorY
     * 
     * @return double[] 
     * */
    public static double[] mercator2LonLat(double mercatorX, double mercatorY){
        double[] xy = new double[2];
        double x = mercatorX/20037508.34*180;
        double y = mercatorY/20037508.34*180;
        y= 180/M_PI*(2*Math.atan(Math.exp(y*M_PI/180))-M_PI/2);
        xy[0] = x;
        xy[1] = y;
        return xy;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("WGS84經緯度轉web Mercator");
        System.out.println("mercatorX:  " + GisUtil.lonLat2Mercator(100, 29)[0]);
        System.out.println("mercatorY:  " + GisUtil.lonLat2Mercator(100, 29)[1]);
        
        System.out.println("web Mercator轉WGS84經緯度");
        System.out.println("lat:  " + GisUtil.mercator2LonLat(1.1131949079327224E7, 3375646.035778616)[0]);
        System.out.println("lon:  " + GisUtil.mercator2LonLat(1.1131949079327224E7, 3375646.035778616)[1]);
    }

}

 


免責聲明!

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



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