百度地圖API(四)

百度地圖呈現

  前面工做已經完成,控件,方法等,這篇主要記錄如何在WPF上呈現,還有座標WGS-84轉換爲百度座標的算法。php

WPF頁面加載

  首先,要引用BaiduControl的項目,在window中引用算法

1 xmlns:BaiduMapControl="clr-namespace:BaiduMapControl;assembly=BaiduMapControl"

  在Grid中聲明,命名爲bmcBaiduMap,在控件上面,還加了一個隱藏的控件,是一個加載等待控件,由於百度地圖API是一Web,沒法在其上添加加載控件,因此就先定義了一個加載控件,設置爲隱藏,加載的時候顯示,加載完成隱藏。ide

1 <Grid>
2                                 <controls:WaitLoading x:Name="LoadingControl"
3                                                       Visibility="Collapsed" />
4                                 <BaiduMapControl:BaiduMap x:Name="bmcBaiduMap"
5                                                           Visibility="Visible"
6                                                           Opacity="0.5"
7                                                           Margin="0,0,0,0" />
8                             </Grid>

地圖上的操做

  這裏只寫了一部分的操做,不過大致一致。google

  地圖上加載一個UserMarkerspa

1 UserMarker um = new UserMarker(selectedUser.UserName);
2 bmcBaiduMap.SelectedUserMarker(um);

  地圖上加載路線code

1 List<BaiduMapControl.MapBase.Point> pointList = new List<BaiduMapControl.MapBase.Point>();
2 //賦值
3 BaiduMapControl.MapService.UserRoute userRoute = new BaiduMapControl.MapService.UserRoute(pointList);
4 bmcBaiduMap.AddUserRoute(userRoute);

  清空全部覆蓋物orm

1 BaiduMapControl.MapBase.MapControl mc = new BaiduMapControl.MapBase.MapControl();
2 bmcBaiduMap.ClearAllOverlay(mc);

  地圖中心移動到某座標xml

1 BaiduMapControl.MapBase.MapControl mc = new BaiduMapControl.MapBase.MapControl(lastPosition.Lng, lastPosition.Lat, mapSize);
2 bmcBaiduMap.MoveMapTo(mc);

座標轉換算法

  分了兩部,第一步是將WGS-84(地球座標系)轉爲GCJ-02(火星座標系),第二部纔是GCJ-02轉爲BD-09(百度座標系)(好像百度地圖官方有直接將WGS-84轉爲百度座標系的接口,能夠自行查找)htm

  地球座標系 (WGS-84) 到火星座標系 (GCJ-02) 的轉換算法blog

 1 #region  地球座標系 (WGS-84) 到火星座標系 (GCJ-02) 的轉換算法
 2 
 3         const double pi = 3.14159265358979324;
 4         //
 5         // Krasovsky 1940
 6         //
 7         // a = 6378245.0, 1/f = 298.3
 8         // b = a * (1 - f)
 9         // ee = (a^2 - b^2) / a^2;
10         const double a = 6378245.0;
11         const double ee = 0.00669342162296594323;
12 
13         /// <summary>
14         ///  World Geodetic System ==> Mars Geodetic System(GPS左邊轉換火星座標)
15         /// </summary>
16         /// <param name="wgLat"></param>
17         /// <param name="wgLon"></param>
18         /// <param name="mgLat"></param>
19         /// <param name="mgLon"></param>
20         public static List<string> TransformGPSToBMap(double wgLat, double wgLon)
21         {
22             List<string> croods = new List<string>();
23             double mgLat;
24             double mgLon;
25             if (outOfChina(wgLat, wgLon))
26             {
27                 mgLat = wgLat;
28                 mgLon = wgLon;
29                 return bd_encrypt(mgLat,mgLon);
30             }
31 
32             double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
33             double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
34             double radLat = wgLat / 180.0 * pi;
35             double magic = Math.Sin(radLat);
36             magic = 1 - ee * magic * magic;
37             double sqrtMagic = Math.Sqrt(magic);
38             dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
39             dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
40             mgLat = wgLat + dLat;
41             mgLon = wgLon + dLon;
42 
43             return bd_encrypt(mgLat, mgLon);
44         }
45 
46         /// <summary>
47         /// 座標是否出中國地區
48         /// </summary>
49         /// <param name="lat"></param>
50         /// <param name="lon"></param>
51         /// <returns></returns>
52         static bool outOfChina(double lat, double lon)
53         {
54             if (lon < 72.004 || lon > 137.8347)
55                 return true;
56             if (lat < 0.8293 || lat > 55.8271)
57                 return true;
58             return false;
59         }
60 
61         /// <summary>
62         /// 轉換緯度
63         /// </summary>
64         /// <param name="x"></param>
65         /// <param name="y"></param>
66         /// <returns></returns>
67         static double transformLat(double x, double y)
68         {
69             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));
70             ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
71             ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
72             ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
73             return ret;
74         }
75 
76         /// <summary>
77         /// 轉換經度
78         /// </summary>
79         /// <param name="x"></param>
80         /// <param name="y"></param>
81         /// <returns></returns>
82         static double transformLon(double x, double y)
83         {
84             double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
85             ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
86             ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
87             ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
88             return ret;
89         }
90 
91         #endregion
View Code

  火星座標系 (GCJ-02) 與百度座標系 (BD-09) 的轉換算法

 1 #region 火星座標系 (GCJ-02) 與百度座標系 (BD-09) 的轉換算法
 2 
 3         const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
 4 
 5         /// <summary>
 6         /// 將 GCJ-02(google地圖) 座標轉換成 BD-09(百度地圖) 座標
 7         /// </summary>
 8         /// <param name="gg_lat"></param>
 9         /// <param name="gg_lon"></param>
10         /// <returns></returns>
11         public static List<string> bd_encrypt(double gg_lat, double gg_lon)
12         {
13             double bd_lat;
14             double bd_lon;
15             double x = gg_lon, y = gg_lat;
16             double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
17             double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
18             bd_lon = z * Math.Cos(theta) + 0.0065;
19             bd_lat = z * Math.Sin(theta) + 0.006;
20             return new List<string>() { bd_lon.ToString(), bd_lat .ToString()};
21         }
22 
23         /// <summary>
24         /// 將 BD-09 座標轉換成  GCJ-02  座標
25         /// </summary>
26         /// <param name="bd_lat"></param>
27         /// <param name="bd_lon"></param>
28         /// <param name="gg_lat"></param>
29         /// <param name="gg_lon"></param>
30         void bd_decrypt(double bd_lat, double bd_lon, double gg_lat, double gg_lon)
31         {
32             double x = bd_lon - 0.0065, y = bd_lat - 0.006;
33             double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
34             double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
35             gg_lon = z * Math.Cos(theta);
36             gg_lat = z * Math.Sin(theta);
37         }
38 
39         #endregion
View Code

結束語

  百度地圖API的使用到這就over,可能有些地方語言和代碼都不太清晰,語文體育老師教的,體育課都是被英語老師佔~其中也參考了好多大牛的文章,如今已經找不到了,對不住他們呀~如下是百度官方網址。

  百度地圖官方網址:http://developer.baidu.com/map/reference/index.php

  百度API實例網址:http://developer.baidu.com/map/jsdemo.htm#h0_6

相關文章
相關標籤/搜索