.Net 百度经纬度转高德

  • Post author:
  • Post category:其他


1、需求

由于我们项目里面的经纬坐标是百度的,而对接的第三方需要的是高德的经纬坐标,两者之间是有位差区别的,不能直接使用,我们需要通过一个算法将百度经纬度转化为高德经纬度,在百度官网上,有java算法,可以实现他们之间的转化,但是没有.Net版本,所以参考java版本,改写一版.Net 版本。

2、实现代码

 1      /// <summary>
 2         /// 百度经纬度转高德,latitude/ longitude 纬度/经度
 3         /// </summary>
 4         /// <param name="lat">维度</param>
 5         /// <param name="lon">经度</param>
 6         /// <returns></returns>
 7         public static double[] baidu2AMap(double lat, double lon)
 8         {
 9             try
10             {
11                 if (lat != 0 && lon != 0)
12                 {
13                     double var4 = 0.006401062D;
14                     double var6 = 0.0060424805D;
15                     double[] var2 = null;
16 
17                     for (int var3 = 0; var3 < 2; ++var3)
18                     {
19                         var2 = new double[2];
20                         double var16 = lon - var4;
21                         double var18 = lat - var6;
22                         double[] var29 = new double[2];
23                         double var24 = Math.Cos(b(var16) + Math.Atan2(var18, var16)) * (a(var18) + Math.Sqrt(var16 * var16 + var18 * var18)) + 0.0065D;
24                         double var26 = Math.Sin(b(var16) + Math.Atan2(var18, var16)) * (a(var18) + Math.Sqrt(var16 * var16 + var18 * var18)) + 0.006D;
25                         var29[1] = (c(var24));
26                         var29[0] = (c(var26));
27                         var2[1] = (c(lon + var16 - var29[1]));
28                         var2[0] = (c(lat + var18 - var29[0]));
29                         var4 = lon - var2[1];
30                         var6 = lat - var2[0];
31                     }
32 
33                     return var2;
34                 }
35             }
36             catch (Exception ex)
37             {
38                 throw ex;
39             }
40 
41             return new double[] { lat, lon };
42         }
43         private static double a(double var0)
44         {
45             return Math.Sin(var0 * 3000.0D * (3.141592653589793D / 180.0D)) * 2.0E-5D;
46         }
47 
48         private static double b(double var0)
49         {
50             return Math.Cos(var0 * 3000.0D * (3.141592653589793D / 180.0D)) * 3.0E-6D;
51         }
52 
53         private static double c(double var0)
54         {
55             return Math.Round(var0, 8);
56         }

转载于:https://www.cnblogs.com/MirZhai/p/10681485.html