Repository navigation
Expand file tree
/
Copy pathMapViewExample.xaml.cs
More file actions
407 lines (353 loc) · 14.6 KB
/
Copy pathMapViewExample.xaml.cs
File metadata and controls
407 lines (353 loc) · 14.6 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFDevelopers.Controls;
namespace WPFDevelopers.Samples.ExampleViews
{
/// <summary>
/// MapViewExample.xaml 的交互逻辑
/// </summary>
public partial class MapViewExample : UserControl
{
private readonly ObservableCollection<Pushpin> _pushpins = new ObservableCollection<Pushpin>();
private readonly MapTrackAnimator _carTrackAnimator = new MapTrackAnimator();
private readonly int _defaultMapStyle = 7;
private string _tencentKey = string.Empty;
private string _tiandituToken = "key";
private string _tiandituLayer = "vec_w";
private string _mapToken = string.Empty;
private readonly List<MapLocation> _routeWaypoints = new List<MapLocation>();
private readonly ObservableCollection<MapPushpinLayer> _pushpinLayers = new ObservableCollection<MapPushpinLayer>();
private Pushpin _carPushpin;
private int _currentMapStyle = 7;
private MapView MapViewControl
{
get { return FindName("mapView") as MapView; }
}
public IEnumerable PushpinArray
{
get { return (IEnumerable)GetValue(PushpinArrayProperty); }
set { SetValue(PushpinArrayProperty, value); }
}
public static readonly DependencyProperty PushpinArrayProperty =
DependencyProperty.Register("PushpinArray", typeof(IEnumerable), typeof(MapViewExample), new PropertyMetadata(null));
public MapViewExample()
{
InitializeComponent();
var pushpins = new List<PushpinModel>();//116.31035086800418,39.97933035921754
pushpins.Add(new PushpinModel { ID = 1, Latitude = 39.97933035921754, Longitude = 116.31035086800418, Title = "Microsoft" });
pushpins.Add(new PushpinModel { ID = 2, Latitude = 40.02306458088111, Longitude = 116.48969274078887, Title = "Alibaba" });//116.48969274078887,40.02306458088111
pushpins.Add(new PushpinModel { ID = 3, Latitude = 40.04593821261656, Longitude = 116.31875661362514, Title = "Xiaomi" });//116.31875661362514,40.04593821261656
pushpins.Add(new PushpinModel { ID = 4, Latitude = 39.91, Longitude = 116.44, Title = "Apple" });
PushpinArray = pushpins;
foreach (var item in pushpins)
{
_pushpins.Add(new Pushpin
{
Latitude = item.Latitude,
Longitude = item.Longitude,
Title = item.Title,
Tag = item.ID
});
}
_routeWaypoints.Add(new MapLocation { Latitude = 31.915951, Longitude = 107.240750 });
_routeWaypoints.Add(new MapLocation { Latitude = 36.6797276003243, Longitude = 118.495410536117 });
_routeWaypoints.Add(new MapLocation { Latitude = 39.785633, Longitude = 116.557292 });
if (MapViewControl != null)
{
MapViewControl.Pushpins = _pushpins;
MapViewControl.PushpinLayers = _pushpinLayers;
_tiandituLayer = _currentMapStyle == 6 ? "img_w" : "vec_w";
MapViewControl.TileSource = new TiandituTileSource(_tiandituLayer, 0, _tiandituToken);
MapViewControl.AnnotationTileSource = new TiandituTileSource(_tiandituLayer == "img_w" ? "cia_w" : "cva_w", 0, _tiandituToken);
//MapViewControl.TileSource = new AMapTileSource(_currentMapStyle);
//MapViewControl.AnnotationTileSource = null;
}
//if (MapSourceSegmented != null)
//{
// MapSourceSegmented.SelectedIndex = 1;
//}
//UpdateTokenInputVisibility();
EnsureCarLayer();
_carPushpin = FindCarPushpin();
if (_carPushpin != null)
{
var start = _routeWaypoints[0];
_carPushpin.Latitude = start.Latitude + 0.0008;
_carPushpin.Longitude = start.Longitude + 0.0013;
_carPushpin.Tag = GetInitialCarHeading();
}
}
private void BtnCar_Click(object sender, RoutedEventArgs e)
{
if (_routeWaypoints.Count < 2 || MapViewControl == null)
{
return;
}
if (_carPushpin == null)
{
_carPushpin = FindCarPushpin();
}
if (_carPushpin != null)
{
MapViewControl.SelectPushpin(_carPushpin, false);
_carTrackAnimator.Start(
MapViewControl,
_carPushpin,
_routeWaypoints,
new MapTrackAnimatorOptions
{
SegmentSteps = 24,
FrameInterval = TimeSpan.FromSeconds(1.2),
FollowMapCenter = true,
FollowZoomLevel = 7,
SelectTargetOnTick = false,
HeadingOffsetDegrees = 0d,
Loop = false
});
}
}
private Pushpin FindCarPushpin()
{
if (MapViewControl == null)
{
return null;
}
if (MapViewControl.PushpinLayers != null)
{
foreach (var layer in MapViewControl.PushpinLayers)
{
if (layer?.Pushpins == null)
{
continue;
}
var car = layer.Pushpins.FirstOrDefault(x => string.Equals(x?.Title, "Car", StringComparison.OrdinalIgnoreCase));
if (car != null)
{
return car;
}
}
}
if (MapViewControl.InlinePushpins != null)
{
return MapViewControl.InlinePushpins.FirstOrDefault(x => string.Equals(x?.Title, "Car", StringComparison.OrdinalIgnoreCase));
}
return null;
}
private void EnsureCarLayer()
{
if (MapViewControl == null)
{
return;
}
var existingCar = FindCarPushpin();
if (existingCar != null)
{
_carPushpin = existingCar;
NormalizeCarPushpin(_carPushpin);
return;
}
var carTemplate = ResolveCarPushpinTemplate();
var carLayer = new MapPushpinLayer
{
Name = "CarLayer",
IsVisible = true,
EnableClustering = false
};
var start = _routeWaypoints.Count > 0 ? _routeWaypoints[0] : new MapLocation { Latitude = 31.915951, Longitude = 107.240750 };
_carPushpin = new Pushpin
{
Title = "Car",
AnchorPoint = new Point(0.5, 0.5),
Latitude = start.Latitude + 0.0008,
Longitude = start.Longitude + 0.0013,
Template = carTemplate,
Tag = GetInitialCarHeading()
};
NormalizeCarPushpin(_carPushpin);
carLayer.Pushpins.Add(_carPushpin);
_pushpinLayers.Add(carLayer);
}
private void NormalizeCarPushpin(Pushpin carPushpin)
{
if (carPushpin == null)
{
return;
}
carPushpin.AnchorPoint = new Point(0.5, 0.5);
carPushpin.Template = ResolveCarPushpinTemplate();
}
private DataTemplate ResolveCarPushpinTemplate()
{
if (MapViewControl != null)
{
var fromMap = MapViewControl.TryFindResource("CarPushpinTemplate") as DataTemplate;
if (fromMap != null)
{
return fromMap;
}
}
var fromControl = TryFindResource("CarPushpinTemplate") as DataTemplate;
if (fromControl != null)
{
return fromControl;
}
return Application.Current?.TryFindResource("CarPushpinTemplate") as DataTemplate;
}
private double GetInitialCarHeading()
{
if (_routeWaypoints.Count < 2)
{
return 0d;
}
return MapTrackAnimator.CalculateHeading(_routeWaypoints[0], _routeWaypoints[1], 0d);
}
private void PART_Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var itemElement = sender as FrameworkElement;
var source = PushpinArray?.OfType<PushpinModel>();
if (itemElement == null || source == null)
{
return;
}
var model = source.FirstOrDefault(x => Equals(x.ID, itemElement.Tag));
if (model == null)
return;
if (MapViewControl == null)
{
return;
}
var pushpin = _pushpins.FirstOrDefault(x => Equals(x.Tag, model.ID));
if (pushpin != null)
{
MapViewControl.SelectPushpin(pushpin, true);
}
MapViewControl.CenterLatitude = model.Latitude;
MapViewControl.CenterLongitude = model.Longitude;
MapViewControl.ZoomLevel = 16;
}
private void StyleSegmented_ItemClick(object sender, RoutedEventArgs e)
{
var segmented = sender as Segmented;
if (segmented == null)
{
return;
}
switch (segmented.SelectedIndex)
{
case 0:
_currentMapStyle = 6;
break;
case 1:
_currentMapStyle = 7;
break;
case 2:
_currentMapStyle = 8;
break;
default:
_currentMapStyle = 7;
break;
}
if (MapViewControl == null)
{
return;
}
//if (MapSourceSegmented != null && MapSourceSegmented.SelectedIndex == 1)
//{
// _tiandituLayer = _currentMapStyle == 6 ? "img_w" : "vec_w";
// MapViewControl.TileSource = new TiandituTileSource(_tiandituLayer, 0, _tiandituToken);
// MapViewControl.AnnotationTileSource = new TiandituTileSource(_tiandituLayer == "img_w" ? "cia_w" : "cva_w", 0, _tiandituToken);
// return;
//}
MapViewControl.TileSource = new AMapTileSource(_currentMapStyle);
MapViewControl.AnnotationTileSource = null;
}
private void MapTokenTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var textBox = sender as TextBox;
//if (textBox == null || MapViewControl == null || MapSourceSegmented == null)
if (textBox == null || MapViewControl == null)
{
return;
}
_mapToken = textBox.Text ?? string.Empty;
_tiandituToken = _mapToken;
MapViewControl.TileSource = new TiandituTileSource(_tiandituLayer, 0, _tiandituToken);
MapViewControl.AnnotationTileSource = new TiandituTileSource(_tiandituLayer == "img_w" ? "cia_w" : "cva_w", 0, _tiandituToken);
//if (MapSourceSegmented.SelectedIndex == 1)
//{
// _tiandituToken = _mapToken;
// MapViewControl.TileSource = new TiandituTileSource(_tiandituLayer, 0, _tiandituToken);
// MapViewControl.AnnotationTileSource = new TiandituTileSource(_tiandituLayer == "img_w" ? "cia_w" : "cva_w", 0, _tiandituToken);
//}
}
//private void MapSourceSegmented_ItemClick(object sender, RoutedEventArgs e)
//{
// if (MapViewControl == null)
// {
// return;
// }
// UpdateTokenInputVisibility();
// switch (MapSourceSegmented.SelectedIndex)
// {
// case 1:
// _mapToken = _tiandituToken;
// MapTokenTextBox.Text = _mapToken;
// _tiandituLayer = _currentMapStyle == 6 ? "img_w" : "vec_w";
// MapViewControl.TileSource = new TiandituTileSource(_tiandituLayer, 0, _tiandituToken);
// MapViewControl.AnnotationTileSource = new TiandituTileSource(_tiandituLayer == "img_w" ? "cia_w" : "cva_w", 0, _tiandituToken);
// break;
// default:
// _mapToken = string.Empty;
// MapTokenTextBox.Text = string.Empty;
// MapViewControl.TileSource = new AMapTileSource(_currentMapStyle);
// MapViewControl.AnnotationTileSource = null;
// break;
// }
//}
//private void UpdateTokenInputVisibility()
//{
// if (MapTokenBorder == null || MapTokenTextBox == null || MapTokenTitle == null || MapSourceSegmented == null)
// {
// return;
// }
// var isVisible = MapSourceSegmented.SelectedIndex == 1;
// MapTokenBorder.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
// if (MapSourceSegmented.SelectedIndex == 1)
// {
// MapTokenTitle.Text = "天地图 Token";
// MapTokenTextBox.Text = _tiandituToken;
// //WPFDevelopers.Helpers.ElementHelper.SetWatermark(MapTokenTextBox, "请输入天地图 token");
// }
// else
// {
// MapTokenTextBox.Text = string.Empty;
// }
//}
private void OpenMapViewDemo_Click(object sender, RoutedEventArgs e)
{
var window = new MapViewFeaturesWindow
{
Owner = Window.GetWindow(this)
};
window.Show();
}
private void MapView_MapFeatureClicked(object sender, RoutedEventArgs e)
{
var featureArgs = e as MapFeatureClickEventArgs;
if (featureArgs == null)
{
return;
}
if (featureArgs.FeatureType == MapFeatureType.Circle && featureArgs.ClickedCircle != null)
{
Toast.Push("此为空域管制区",ToastImage.Warning,true);
}
}
}
}