各個座標系的概況:請參考:http://blog.csdn.net/findsafety/article/details/12442639git
1、座標轉換算法的實現。算法
/// <summary>
/// WGS84座標轉GCJ02座標
/// </summary>
public class GDMapChange { public GDMapChange(float lat, float lon) { this.latitude = lat; this.longitude = lon; } const double pi = 3.14159265358979324; static double a = 6378245.0; static double ee = 0.00669342162296594323;
public double latitude { get; set; } public double longitude { get; set; } private Boolean outOfChina(double lat, double lon) { if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return false; } private double transformLat(double x, double y) { var 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 * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0; ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0; return ret; } private double transformLon(double x, double y) { var 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 * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0; ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0; return ret; } /// <summary> /// /// </summary> /// <param name="lat"></param> /// <param name="lon"></param> /// <returns></returns> private GDMapChange delta(float lat, float lon) { //var a = 6378245.0; //var ee = 0.00669342162296594323; var dLat = this.transformLat(lon - 105.0, lat - 35.0); var dLon = this.transformLon(lon - 105.0, lat - 35.0); var radLat = lat / 180.0 * pi; var magic = Math.Sin(radLat); magic = 1 - ee * magic * magic; var sqrtMagic = Math.Sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi); GDMapChange gps = new GDMapChange((float)dLat, (float)dLon); return gps; } public GDMapChange gcj_encrypt(float wgsLat, float wgsLon) { GDMapChange d = this.delta(wgsLat, wgsLon); d.latitude = wgsLat + d.latitude; d.longitude = wgsLon + d.longitude; return d; } }
此方法經過算法算出具體偏移量,再與原Gps座標相加,就是轉換以後的座標結果,經驗證,誤差不大。
二、調用座標轉換接口。(更多的信息詳情請查閱GPSspg API)api
http://www.gpsspg.com/api/convert/latlng/post
#region 偏移處理(將設備上傳的GPS座標轉換爲火星座標) /// <summary> /// 偏移處理 /// </summary> /// <param name="lng">經度</param> /// <param name="lat">緯度</param> /// <returns></returns> public BaiduMapGps GetBaiduMap(float lng, float lat) { Encoding encoding = Encoding.GetEncoding("UTF-8");
//oid和key 經過申請能夠獲得 參數說明請看API文檔 string url = "http://api.gpsspg.com/convert/latlng/?oid=yout OID&key=your key&from=0&to=3&latlng=" + lat + "," + lng; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); //也能夠是post請求 request.Method = WebRequestMethods.Http.Get; //發送http請求及獲得請求響應 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { //使用讀取流讀取響應結果 using (StreamReader reader = new StreamReader(response.GetResponseStream())) { BaiduMapGps baidumapgps = new BaiduMapGps(); string content = reader.ReadToEnd(); var jobject = JObject.Parse(content); JArray jarry1 = JArray.Parse(jobject["result"].ToString()); for (var k = 0; k < jarry1.Count; k++) { JObject jk = JObject.Parse(jarry1[k].ToString()); baidumapgps.y = (float)Convert.ToDecimal(jk["lat"].ToString()); baidumapgps.x = (float)Convert.ToDecimal(jk["lng"].ToString()); } return baidumapgps; } } } #endregion