Gps坐标转换成火星坐标的两种方法
生活随笔
收集整理的這篇文章主要介紹了
Gps坐标转换成火星坐标的两种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
各個坐標系的概況:請參考: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
總結
以上是生活随笔為你收集整理的Gps坐标转换成火星坐标的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pfx格式密钥库修改密码
- 下一篇: AMD W7600 / W7500 专业