百度地图API(二)

百度地图控件BaiduMap

  这篇主要讲地图控件的定义和方法的实现。

页面

  在用户控件BaiduMap.xaml中定义一个WebBrowser

1 <WebBrowser Margin="0,0,0,0"
2                     x:Name="webBrowser"/>

后台

  在BaiduMap.xaml.cs文件中加载百度地图,并定义各种方法。

 1         /// <summary>
 2         /// 计时器
 3         /// </summary>
 4         private Timer timer = new Timer();
 5 
 6         /// <summary>
 7         /// 坐标
 8         /// </summary>
 9         //private MapBase.Point point;
10 
11         /// <summary>
12         /// 文件地址
13         /// </summary>
14         string path = System.Windows.Forms.Application.StartupPath + "/HttpMap/BMap.html";
15         //string path = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "/HttpMap/BMap.html";
16         
17         //string DefaultSource = Application.StartupPath + "\HttpMap\BMap.html";
18 
19         public BaiduMap()
20         {
21             InitializeComponent();
22             // 进行处理,成为可信的脚本文件
23             string DefaultSource = "file://127.0.0.1/" + path.Replace("\", @"/").Replace(":","").Insert(1, "$");
24             Uri uri = new Uri(DefaultSource);
25             // 加载html
26             webBrowser.Navigate(uri);
27             
28             timer.Interval = 1000;
29             timer.Start();
30             timer.Elapsed += timer_Elapsed;
31         }
View Code

  代码中,注释掉很多关于路径的定义,原因就是将所加载的脚本文件设置为可信,否则,百度地图界面上总会出现类似“是否加载该脚本”的提示语。path的指定的路径是程序开始的路径下的httpMap文件夹下的BMap.html文件。

Methods

  定义的方法比较多,就分开写了~

 1         #region 选择人员
 2 
 3         /// <summary>
 4         /// 选择人员
 5         /// </summary>
 6         /// <param name="_userMarker"></param>
 7         [Description("选择人员")]
 8         public void SelectedUserMarker(UserMarker _userMarker)
 9         {
10             _userMarker.SelectedUserMarker(this.webBrowser);
11         }
12 
13         #endregion
14 
15         #region 地图移到某坐标
16 
17         [Description("地图移到指定坐标")]
18         public void MoveMapTo(MapControl mc)
19         {
20             mc.MoveMapTo(this.webBrowser);
21         }
22 
23         #endregion
24 
25         #region 清除所有覆盖物
26 
27         [Description("清除所有覆盖物")]
28         public void ClearAllOverlay(MapControl mc)
29         {
30             mc.ClearAllOverlay(this.webBrowser);
31         }
32 
33         #endregion
34 
35         #region  添加路径
36 
37         /// <summary>
38         /// 获取当前鼠标所在坐标
39         /// </summary>
40         /// <returns></returns>
41         [Description("获取当前鼠标所在坐标")]
42         public void AddUserRoute(UserRoute ur)
43         {
44             ur.AddUserRoute(this.webBrowser);
45         }
46 
47         #endregion
48 
49         #region 清空所有路径
50 
51         /// <summary>
52         /// 清空所有路径
53         /// </summary>
54         [Description("清空所有路径")]
55         public void ClearAllUserRoute(int userCount)
56         {
57             this.webBrowser.InvokeScript("ClearAllUserRoute",new Object[]{userCount});
58         }
59 
60         #endregion
61 
62         #region 添加地址,以及连接到marker
63 
64         /// <summary>
65         /// 地址连接
66         /// </summary>
67         /// <param name="geo"></param>
68         [Description("地址连接")]
69         public void GetAddresses(Geocoder geo)
70         {
71             //geo.GetAddresses(this.webBrowser);
72             geo.GetAddressList(this.webBrowser);
73         }
74 
75         #endregion
76 
77         #region 添加一个标签
78 
79         /// <summary>
80         /// 添加一个标签
81         /// </summary>
82         /// <param name="_label"></param>
83         [Description("添加一个标签")]
84         public void AddLebel(MapLayer.Label _label)
85         {
86             _label.AddLabel(this.webBrowser);
87         }
88 
89         #endregion
View Code
  1         #region  获取当前鼠标所在坐标
  2 
  3         /// <summary>
  4         /// 获取当前鼠标所在坐标
  5         /// </summary>
  6         /// <returns></returns>
  7         [Description("获取当前鼠标所在坐标")]
  8         public BaiduMapControl.MapBase.Point CallBackPoint()
  9         {
 10             timer.Enabled = true;
 11             return null ;
 12         }
 13 
 14         #endregion
 15 
 16         #region 将地图放大一级
 17 
 18         /// <summary>
 19         /// 将地图放大一级
 20         /// </summary>
 21         public void ZoomIn()
 22         {
 23             this.webBrowser.InvokeScript("ZoomIn");
 24         }
 25 
 26         #endregion
 27 
 28         #region 范围搜索
 29 
 30         /// <summary>
 31         /// 范围搜索
 32         /// </summary>
 33         /// <param name="keywords"></param>
 34         [Description("范围搜索")]
 35         public void SearchInBounds(string keywords)
 36         {
 37             LocalSearch local = new LocalSearch();
 38             local.SearchInBounds(keywords, this.webBrowser);
 39         }
 40 
 41         #endregion
 42 
 43         #region 驾车导航
 44 
 45         /// <summary>
 46         /// 获取指定起点和终点的驾车导航
 47         /// </summary>
 48         /// <param name="d"></param>
 49         ///  使用方法:
 50         ///  DrivingRoute d = new DrivingRoute("王府井", "中关村");
 51         ///  BMap.GetDrivingRoute(t);
 52         [Description("获取指定起点和终点的驾车导航")]
 53         public void GetDrivingRoute(DrivingRoute d)
 54         {
 55             d.GetDrivingRoute(this.webBrowser);
 56         }
 57 
 58         #endregion
 59 
 60         #region 公交导航
 61 
 62         /// <summary>
 63         /// 获得指定起点和终点的公交导航
 64         /// </summary>
 65         /// <param name="t"></param>
 66         /// 使用方法:
 67         /// TransitRoute t = new TransitRoute("王府井", "中关村");
 68         /// BMap.GetTransitRoute(t);
 69         [Description("获得指定起点和终点的公交导航")]
 70         public void GetTransitRoute(TransitRoute t)
 71         {
 72             t.GetTransitRoute(this.webBrowser);
 73         }
 74 
 75         #endregion
 76 
 77         #region 步行导航
 78 
 79         /// <summary>
 80         /// 获取指定起点和终点的步行导航
 81         /// </summary>
 82         /// <param name="w"></param>
 83         ///  使用方法:
 84         ///  WalkingRoute w = new WalkingRoute("王府井", "中关村");
 85         ///  BMap.GetWalkingRoute(w);
 86         [Description("获取指定起点和终点的步行导航")]
 87         public void GetWalkingRoute(WalkingRoute w)
 88         {
 89             w.GetWalkingRoute(this.webBrowser);
 90         }
 91 
 92         #endregion
 93 
 94         #region 地址解析
 95 
 96         /// <summary>
 97         /// 返回指定地址所在的经纬度坐标
 98         /// </summary>
 99         /// <param name="g">地理解析实例</param>
100         /// <returns>坐标点</returns>
101         /// 使用方法:
102         //Geocoder g = new Geocoder("宁夏回族自治区");
103         //BMap.DecodeAddress(g);
104         //[Description("返回指定地址所在的经纬度坐标")]
105         //public MapBase.Point DecodeAddress(Geocoder g)
106         //{
107         //    return g.DecodeAddress(this.webBrowser);
108         //}
109 
110         #endregion
View Code
 1         #region 添加一个圆
 2 
 3         /// <summary>
 4         /// 添加一个圆
 5         /// </summary>
 6         /// <param name="cirle"></param>
 7         [Description("添加一个圆")]
 8         public void AddCirle(Cirle cirle)
 9         {
10             cirle.AddCirle(this.webBrowser);
11         }
12 
13         #endregion
14 
15         #region 设置中心城市
16 
17         /// <summary>
18         /// 设置中心城市
19         /// </summary>
20         /// <param name="_cityName">城市名称</param>
21         /// 使用方法:BaiduM.SetCity("北京")
22         [Description("设置中心城市")]
23         public void SetCity(string _cityName)
24         {
25             webBrowser.InvokeScript("SetCity", new Object[] { _cityName });
26         }
27 
28         #endregion
29 
30         #region  返回地图中心坐标
31 
32 
33         #endregion
34 
35         #region 添加一个地图控件
36 
37         /// <summary>
38         /// 添加地图控件
39         /// </summary>
40         /// <param name="m">地图控件</param>
41         /// 使用方法:
42         /// MapControl m = new MapControl(MapControlType.MapTypeControl);
43         /// Bmap.AddMapControl(m);
44         [Description("添加一个地图控件")]
45         public void AddMapControl(MapControl _m)
46         {
47             _m.AddControl(this.webBrowser);
48         }
49 
50         #endregion
51 
52         #region 添加标注点
53 
54         /// <summary>
55         /// 添加标注点
56         /// </summary>
57         /// <param name="maker">标注点</param>
58         /// 使用方法:
59         ///Maker maker = new Maker(new MapBase.Point("119.2345", "36.1248"), MakerType.Normal);
60         ///BaiduMap.AddMaker(maker);
61         [Description("添加一个标注点")]
62         public void AddMaker(Maker maker)
63         {
64             maker.AddMaker(this.webBrowser);
65         }
66 
67         #endregion
68 
69         #region 添加一个信息窗口
70 
71         /// <summary>
72         /// 添加一个信息窗口
73         /// </summary>
74         /// <param name="info"></param>
75         /// 使用方法:
76         /// InfoWindow info = new InfoWindow("这是一个信息窗口", new Point("119.3612", "36.1245"));
77         /// BaiduMap.AddInfoWindow(Info);
78         [Description("添加一个信息窗口")]
79         public void AddInfoWindow(InfoWindow _info)
80         {
81             _info.AddInfoWindow(this.webBrowser);
82         }
83 
84         #endregion
View Code

  最后是一个定时器的方法,具体想做什么可以添加

 1 void timer_Elapsed(object sender, ElapsedEventArgs e)
 2         {
 3             try
 4             {
 5 
 6             }
 7             catch (Exception)
 8             {
 9             }
10         }
View Code

Waiting

  百度地图控件定义完成,下一篇是html和js~明天吧~

原文地址:https://www.cnblogs.com/shadow-fei/p/4680371.html