百度地圖API(二)

百度地圖控件BaiduMap

  這篇主要講地圖控件的定義和方法的實現。html

頁面

  在用戶控件BaiduMap.xaml中定義一個WebBrowserweb

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

後臺

  在BaiduMap.xaml.cs文件中加載百度地圖,並定義各類方法。ide

 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文件。this

Methods

  定義的方法比較多,就分開寫了~spa

 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

  最後是一個定時器的方法,具體想作什麼能夠添加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~明天吧~orm

相關文章
相關標籤/搜索