Web墨卡託座標與WGS84經緯度互轉 java程式碼

我就是曹總發表於2019-05-11

package com.util;

public class Coordinate {

	static double M_PI = Math.PI;

	// 經緯度轉墨卡託
	// 經度(lon),緯度(lat)
	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;
	}

	// 墨卡託轉經緯度
	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;
	}

	public static void main(String[] args) {
//		 num;
		 double[] num = lonLat2Mercator(120.385222, 36.061416);
		System.err.println(num[0]+"  "+num[1]);
//		for (int i = 0; i < num.length; i++) {
//			System.out.println(num[0]+"  "+num[1]);
//		}
		// num = Mercator2lonLat(13401221.612075035,4309075.414032666);
		// for(int i=0;i<num.length;i++)
		// {
		// System.out.println(num[i]);
		// }
	}

}



內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章