-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathGMapOverlay.cs
More file actions
494 lines (430 loc) · 14.8 KB
/
GMapOverlay.cs
File metadata and controls
494 lines (430 loc) · 14.8 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using System;
using System.Drawing;
using System.Runtime.Serialization;
using GMap.NET.ObjectModel;
namespace GMap.NET.WindowsForms
{
/// <summary>
/// GMap.NET overlay
/// </summary>
[Serializable]
public class GMapOverlay : ISerializable, IDeserializationCallback, IDisposable
{
bool _isVisibile = true;
/// <summary>
/// is overlay visible
/// </summary>
public bool IsVisibile
{
get
{
return _isVisibile;
}
set
{
if (value != _isVisibile)
{
_isVisibile = value;
if (Control != null)
{
if (_isVisibile)
{
Control.HoldInvalidation = true;
{
ForceUpdate();
}
Control.Refresh();
}
else
{
if (Control.IsMouseOverMarker)
{
Control.IsMouseOverMarker = false;
}
if (Control.IsMouseOverPolygon)
{
Control.IsMouseOverPolygon = false;
}
if (Control.IsMouseOverRoute)
{
Control.IsMouseOverRoute = false;
}
Control.RestoreCursorOnLeave();
if (!Control.HoldInvalidation)
{
Control.Invalidate();
}
}
}
}
}
}
bool _isHitTestVisible = true;
/// <summary>
/// HitTest visibility for entire overlay
/// </summary>
public bool IsHitTestVisible
{
get { return _isHitTestVisible; }
set { _isHitTestVisible = value; }
}
bool _isZoomSignificant = true;
/// <summary>
/// if false don't consider contained objects when box zooming
/// </summary>
public bool IsZoomSignificant
{
get { return _isZoomSignificant; }
set { _isZoomSignificant = value; }
}
/// <summary>
/// overlay Id
/// </summary>
public string Id;
/// <summary>
/// list of markers, should be thread safe
/// </summary>
public readonly ObservableCollectionThreadSafe<GMapMarker> Markers =
new ObservableCollectionThreadSafe<GMapMarker>();
/// <summary>
/// list of routes, should be thread safe
/// </summary>
public readonly ObservableCollectionThreadSafe<GMapRoute> Routes =
new ObservableCollectionThreadSafe<GMapRoute>();
/// <summary>
/// list of polygons, should be thread safe
/// </summary>
public readonly ObservableCollectionThreadSafe<GMapPolygon> Polygons =
new ObservableCollectionThreadSafe<GMapPolygon>();
GMapControl _control;
public GMapControl Control
{
get
{
return _control;
}
internal set
{
_control = value;
}
}
public GMapOverlay()
{
CreateEvents();
}
public GMapOverlay(string id)
{
Id = id;
CreateEvents();
}
void CreateEvents()
{
Markers.CollectionChanged += Markers_CollectionChanged;
Routes.CollectionChanged += Routes_CollectionChanged;
Polygons.CollectionChanged += Polygons_CollectionChanged;
}
void ClearEvents()
{
Markers.CollectionChanged -= Markers_CollectionChanged;
Routes.CollectionChanged -= Routes_CollectionChanged;
Polygons.CollectionChanged -= Polygons_CollectionChanged;
}
public void Clear()
{
Markers.Clear();
Routes.Clear();
Polygons.Clear();
}
void Polygons_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (GMapPolygon obj in e.NewItems)
{
if (obj != null)
{
obj.Overlay = this;
if (Control != null)
{
Control.UpdatePolygonLocalPosition(obj);
}
}
}
}
if (Control != null)
{
if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
{
if (Control.IsMouseOverPolygon)
{
Control.IsMouseOverPolygon = false;
Control.RestoreCursorOnLeave();
}
}
if (!Control.HoldInvalidation)
{
Control.Invalidate();
}
}
}
void Routes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (GMapRoute obj in e.NewItems)
{
if (obj != null)
{
obj.Overlay = this;
if (Control != null)
{
Control.UpdateRouteLocalPosition(obj);
}
}
}
}
if (Control != null)
{
if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
{
if (Control.IsMouseOverRoute)
{
Control.IsMouseOverRoute = false;
Control.RestoreCursorOnLeave();
}
}
if (!Control.HoldInvalidation)
{
Control.Invalidate();
}
}
}
void Markers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (GMapMarker obj in e.NewItems)
{
if (obj != null)
{
obj.Overlay = this;
if (Control != null)
{
Control.UpdateMarkerLocalPosition(obj);
}
}
}
}
if (Control != null)
{
if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
{
if (Control.IsMouseOverMarker)
{
Control.IsMouseOverMarker = false;
Control.RestoreCursorOnLeave();
}
}
if (!Control.HoldInvalidation)
{
Control.Invalidate();
}
}
}
/// <summary>
/// updates local positions of objects
/// </summary>
internal void ForceUpdate()
{
if (Control != null)
{
foreach (var obj in Markers)
{
if (obj.IsVisible)
{
Control.UpdateMarkerLocalPosition(obj);
}
}
foreach (var obj in Polygons)
{
if (obj.IsVisible)
{
Control.UpdatePolygonLocalPosition(obj);
}
}
foreach (var obj in Routes)
{
if (obj.IsVisible)
{
Control.UpdateRouteLocalPosition(obj);
}
}
}
}
/// <summary>
/// renders objects/routes/polygons
/// </summary>
/// <param name="g"></param>
public virtual void OnRender(Graphics g)
{
if (Control != null)
{
if (Control.RoutesEnabled)
{
foreach (var r in Routes)
{
if (r.IsVisible)
{
r.OnRender(g);
}
}
}
if (Control.PolygonsEnabled)
{
foreach (var r in Polygons)
{
if (r.IsVisible)
{
r.OnRender(g);
}
}
}
if (Control.MarkersEnabled)
{
// markers
foreach (var m in Markers)
{
//if(m.IsVisible && (m.DisableRegionCheck || Control.Core.currentRegion.Contains(m.LocalPosition.X, m.LocalPosition.Y)))
if (m.IsVisible || m.DisableRegionCheck)
{
m.OnRender(g);
}
}
// ToolTips are drawn in separate method to ensure they are top most
}
}
}
public virtual void OnRenderToolTips(Graphics g)
{
if (Control != null)
{
if (Control.MarkersEnabled)
{
// tooltips above
foreach (var m in Markers)
{
//if(m.ToolTip != null && m.IsVisible && Control.Core.currentRegion.Contains(m.LocalPosition.X, m.LocalPosition.Y))
if (m.ToolTip != null && m.IsVisible)
{
if (!string.IsNullOrEmpty(m.ToolTipText) &&
(m.ToolTipMode == MarkerTooltipMode.Always ||
m.ToolTipMode == MarkerTooltipMode.OnMouseOver && m.IsMouseOver))
{
m.ToolTip.OnRender(g);
}
}
}
}
}
}
#region ISerializable Members
/// <summary>
/// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with the data needed to serialize the
/// target object.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to populate with data.</param>
/// <param name="context">
/// The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this
/// serialization.
/// </param>
/// <exception cref="T:System.Security.SecurityException">
/// The caller does not have the required permission.
/// </exception>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Id", Id);
info.AddValue("IsVisible", IsVisibile);
var markerArray = new GMapMarker[Markers.Count];
Markers.CopyTo(markerArray, 0);
info.AddValue("Markers", markerArray);
var routeArray = new GMapRoute[Routes.Count];
Routes.CopyTo(routeArray, 0);
info.AddValue("Routes", routeArray);
var polygonArray = new GMapPolygon[Polygons.Count];
Polygons.CopyTo(polygonArray, 0);
info.AddValue("Polygons", polygonArray);
}
private GMapMarker[] deserializedMarkerArray;
private GMapRoute[] deserializedRouteArray;
private GMapPolygon[] deserializedPolygonArray;
/// <summary>
/// Initializes a new instance of the <see cref="GMapOverlay" /> class.
/// </summary>
/// <param name="info">The info.</param>
/// <param name="context">The context.</param>
protected GMapOverlay(SerializationInfo info, StreamingContext context)
{
Id = info.GetString("Id");
IsVisibile = info.GetBoolean("IsVisible");
deserializedMarkerArray = Extensions.GetValue(info, "Markers", new GMapMarker[0]);
deserializedRouteArray = Extensions.GetValue(info, "Routes", new GMapRoute[0]);
deserializedPolygonArray = Extensions.GetValue(info, "Polygons", new GMapPolygon[0]);
CreateEvents();
}
#endregion
#region IDeserializationCallback Members
/// <summary>
/// Runs when the entire object graph has been deserialized.
/// </summary>
/// <param name="sender">
/// The object that initiated the callback. The functionality for this parameter is not currently
/// implemented.
/// </param>
public void OnDeserialization(object sender)
{
// Populate Markers
foreach (var marker in deserializedMarkerArray)
{
marker.Overlay = this;
Markers.Add(marker);
}
// Populate Routes
foreach (var route in deserializedRouteArray)
{
route.Overlay = this;
Routes.Add(route);
}
// Populate Polygons
foreach (var polygon in deserializedPolygonArray)
{
polygon.Overlay = this;
Polygons.Add(polygon);
}
}
#endregion
#region IDisposable Members
bool _disposed;
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
ClearEvents();
foreach (var m in Markers)
{
m.Dispose();
}
foreach (var r in Routes)
{
r.Dispose();
}
foreach (var p in Polygons)
{
p.Dispose();
}
Clear();
}
}
#endregion
}
}