首页 > 技术文章 > WGS84经纬度 与 web 墨卡托相互转化 工具类

zsslll 2017-03-21 15:20 原文

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]);
    }

}

 

推荐阅读