标签:
前面工作已经完成,控件,方法等,这篇主要记录如何在WPF上呈现,还有坐标WGS-84转换为百度坐标的算法。
首先,要引用BaiduControl的项目,在window中引用
1 xmlns:BaiduMapControl="clr-namespace:BaiduMapControl;assembly=BaiduMapControl"
在Grid中声明,命名为bmcBaiduMap,在控件上面,还加了一个隐藏的控件,是一个加载等待控件,因为百度地图API是一Web,无法在其上添加加载控件,所以就先定义了一个加载控件,设置为隐藏,加载的时候显示,加载完成隐藏。
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>
这里只写了一部分的操作,不过大体一致。
地图上加载一个UserMarker
1 UserMarker um = new UserMarker(selectedUser.UserName); 2 bmcBaiduMap.SelectedUserMarker(um);
地图上加载路线
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);
清空所有覆盖物
1 BaiduMapControl.MapBase.MapControl mc = new BaiduMapControl.MapBase.MapControl(); 2 bmcBaiduMap.ClearAllOverlay(mc);
地图中心移动到某坐标
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转为百度坐标系的接口,可以自行查找)
地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法
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
火星坐标系 (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
百度地图API的使用到这就over,可能有些地方语言和代码都不太清晰,语文体育老师教的,体育课都是被英语老师占~其中也参考了好多大牛的文章,现在已经找不到了,对不住他们呀~以下是百度官方网址。
百度地图官方网址:http://developer.baidu.com/map/reference/index.php
百度API实例网址:http://developer.baidu.com/map/jsdemo.htm#h0_6
标签:
原文地址:http://www.cnblogs.com/shadow-fei/p/4683393.html