1. 程式人生 > >墨卡託和經緯度座標互相轉換

墨卡託和經緯度座標互相轉換

public class Point {
    private double x;
    private double y;


    public double getX() {
        return x;
    }


    public void setX(double x) {
        this.x = x;
    }


    public double getY() {
        return y;
    }


    public void setY(double y) {
        this.y = y;
    }

}

public class CoordinateConversion {


/**
* 經緯度轉墨卡託
* @param LonLat 經緯度座標
* @return
*/
public static Point lonLatToMercator(Point LonLat){
Point mercator = new Point();
double x =  (LonLat.getX() * 20037508.342789 / 180);
double y =  (Math.log(Math.tan((90 + LonLat.getY()) * Math.PI / 360)) / (Math.PI / 180));
y =  (double)(y * 20037508.342789 / 180);
mercator.setX(x);
mercator.setY(y);
return mercator;
}

/**
* 墨卡託轉經緯度
* @param mercator 墨卡託座標
* @return
*/
public static Point mercatorToLonLat(Point mercator){
Point lonlat = new Point();
double x =   (mercator.getX() / 20037508.342789 * 180);
double y =  (mercator.getY() / 20037508.342789 * 180);
y = (double) (180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2));
lonlat.setX(x);
lonlat.setY(y);
return lonlat;
}
}