高德SDK獲取到的座標轉換爲GPS真實座標方法,Java版

發現高德SDK不提供高德的座標轉GPS座標(GCJ_02轉WGS_84),下面是一份Java版的java

/****************************
 文件名:GCJ2WGS.java
 建立時間:
 所在包:com
 做者:
 說明:該類的delta方法能夠將高德地圖SDK獲取到的GPS經緯度轉換爲真實的經緯度。
 ****************************/
package com;

import java.util.HashMap;

public class GCJ2WGS {

	public static void main(String[] args) {
		
		GCJ2WGS wg = new GCJ2WGS();
		
		HashMap<String, Double> hm = wg.delta(38.123456,114.654321);
		
		System.out.println(hm);
	}
	//圓周率 GCJ_02_To_WGS_84
	double PI = 3.14159265358979324;
	/**
	 * @author 做者:
	 * 方法描述:方法能夠將高德地圖SDK獲取到的GPS經緯度轉換爲真實的經緯度,能夠用於解決安卓系統使用高德SDK獲取經緯度的轉換問題。
	 * @param 須要轉換的經緯度
	 * @return 轉換爲真實GPS座標後的經緯度
	 * @throws <異常類型> {@inheritDoc} 異常描述
	 */
	public HashMap<String, Double> delta(double lat,double lon) {
		double a = 6378245.0;//克拉索夫斯基橢球參數長半軸a
		double ee = 0.00669342162296594323;//克拉索夫斯基橢球參數第一偏愛率平方
		double dLat = this.transformLat(lon - 105.0, lat - 35.0);
		double dLon = this.transformLon(lon - 105.0, lat - 35.0);
		double radLat = lat / 180.0 * this.PI;
		double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
        
        HashMap<String, Double> hm = new HashMap<String, Double>();
        hm.put("lat",lat - dLat);
        hm.put("lon",lon - dLon);
        
        return hm;
    }
	//轉換經度
	public double transformLon(double x, double y) {
		double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
        return ret;
    }
	//轉換緯度
	public double transformLat(double x, double y) {
		double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

}
相關文章
相關標籤/搜索