各个坐标系的概况:请参考:http://blog.csdn.net/findsafety/article/details/12442639
一、坐标转换算法的实现。
/// <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)
http://www.gpsspg.com/api/convert/latlng/
#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