-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBPlacesBoard.cs
208 lines (202 loc) · 6.87 KB
/
BPlacesBoard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMap.NET.HTTPService;
using Newtonsoft.Json.Linq;
using BMap.NET.WindowsForm.BMapElements;
namespace BMap.NET.WindowsForm
{
/// <summary>
/// 位置列表控件
/// </summary>
public partial class BPlacesBoard : UserControl
{
/// <summary>
/// 与之关联的地图控件
/// </summary>
[Description("与之关联的地图控件"),Category("BMap.NET")]
public BMapControl BMapControl
{
get;
set;
}
/// <summary>
/// 与之关联的导航面板控件
/// </summary>
[Description("与之关联的导航控件"),Category("BMap.NET")]
public BDirectionBoard BDirectionBoard
{
get;
set;
}
/// <summary>
/// 当前建议搜索城市
/// </summary>
[Description("当前建议搜索城市"),Category("BMap.NET")]
public string CurrentCity
{
get;
set;
}
private Label _wait = new Label(); //等待框
/// <summary>
/// 构造方法
/// </summary>
public BPlacesBoard()
{
InitializeComponent();
_wait.AutoSize = false; _wait.Font = new System.Drawing.Font("微软雅黑", 10);
_wait.Width = Width; _wait.Height = Height; _wait.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
_wait.BackColor = Color.FromArgb(200, Color.White);
_wait.TextAlign = ContentAlignment.MiddleCenter;
_wait.Text = "正在搜索,请稍候...";
_wait.Visible = false;
_wait.Location = new Point(0, 0);
Controls.Add(_wait);
_wait.BringToFront();
}
/// <summary>
/// 清空所有位置(初始化控件)
/// </summary>
public void Clear()
{
flpPlaces.Controls.Clear(); //清空所有位置
flpPlaces.Controls.Add(bQuickSearch); //重新加载快速搜索控件
if (BMapControl != null)
{
BMapControl.ClearPlaces();
}
}
/// <summary>
/// 将位置显示在列表中
/// </summary>
/// <param name="places"></param>
internal void AddPlaces(JToken places)
{
flpPlaces.Controls.Clear(); //清空原来所有位置
if (Parent != null && Parent is TabPage) //如果父控件是tabpage 则选中
{
((Parent as TabPage).Parent as TabControl).SelectedTab = (Parent as TabPage);
}
if (BDirectionBoard != null) //导航控件初始化
{
BDirectionBoard.Clear();
}
//加载位置项
int index = 0; List<BPOI> _list = new List<BPOI>();
foreach (JObject place in places)
{
if (place["location"] != null && place["location"]["lng"] != null && (string)place["location"]["lng"] != "")
{
LatLngPoint location = new LatLngPoint(double.Parse((string)place["location"]["lng"]), double.Parse((string)place["location"]["lat"]));
BPOI poi = new BPOI { DataSource = place, Index = index, Selected = false, Location = location };
_list.Add(poi);
BPlaceItem item = new BPlaceItem();
item.Index = index++;
item.POI = poi;
item.SetDestinationPlace += new SetDestinationPlaceEventHandler(item_SetDestinationPlace);
item.SetSourcePlace += new SetSourcePlaceEventHandler(item_SetSourcePlace);
item.PlaceSelectedChanged += new PlaceSelectedChangedEventHandler(item_PlaceSelectedChanged);
item.Height = 104;
flpPlaces.Controls.Add(item);
}
}
if (BMapControl != null)
{
BMapControl.AddPlaces(_list);
}
}
/// <summary>
/// 选中POI
/// </summary>
internal void SelectPlace(BPOI poi)
{
foreach (Control c in flpPlaces.Controls)
{
if (c is BPlaceItem)
{
BPlaceItem p = c as BPlaceItem;
if (p.POI == poi)
{
p.Selected = true;
flpPlaces.ScrollControlIntoView(c);
}
else
{
p.Selected = false;
}
}
}
}
/// <summary>
/// 点击快速搜索面板上的按钮
/// </summary>
/// <param name="searchName"></param>
private void bQuickSearch_QuickSearch(string searchName)
{
_wait.Visible = true;
((Action)delegate()
{
PlaceService ps = new PlaceService();
JObject places = ps.SearchInCity(searchName, CurrentCity);
if (places != null)
{
this.Invoke((Action)delegate()
{
_wait.Visible = false;
flpPlaces.Controls.Remove(bQuickSearch);
AddPlaces(places["results"]); //具体json格式参见api文档
});
}
}).BeginInvoke(null, null);
}
/// <summary>
/// 选中位置改变
/// </summary>
/// <param name="poi"></param>
void item_PlaceSelectedChanged(BPOI poi)
{
//BMapControl
foreach (Control c in flpPlaces.Controls)
{
if (c is BPlaceItem && (c as BPlaceItem).POI != poi)
{
(c as BPlaceItem).Selected = false;
}
}
if (BMapControl != null)
{
BMapControl.SelectBPOI(poi);
}
}
/// <summary>
/// 设置起点
/// </summary>
/// <param name="sourceName"></param>
void item_SetSourcePlace(string sourceName)
{
//BDirecttionBoard
if (BDirectionBoard != null)
{
BDirectionBoard.SourcePlace = sourceName;
}
}
/// <summary>
/// 设置终点
/// </summary>
/// <param name="destinationName"></param>
void item_SetDestinationPlace(string destinationName)
{
//BDirectionBoard
if (BDirectionBoard != null)
{
BDirectionBoard.DestinationPlace = destinationName;
}
}
}
}