-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMap.pas
More file actions
502 lines (415 loc) · 17 KB
/
Copy pathMap.pas
File metadata and controls
502 lines (415 loc) · 17 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
495
496
497
498
499
500
501
502
unit Map;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Normal, Vcl.StdCtrls,
VCL.TMSFNCMaps,
Vcl.ExtCtrls, Miscellaneous, Source, Misc, VCL.TMSFNCTypes,
VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes,
VCL.TMSFNCMapsCommonTypes, VCL.TMSFNCCustomControl, VCL.TMSFNCWebBrowser;
type
TFollowMode = (fmInit, fmNone, fmCar, fmPayload);
type
TBalloon = record
InUse: Boolean;
FNCMarker: TTMSFNCMapsMarker;
MarkerName: String;
FNCLanding: TTMSFNCMapsMarker;
FNCRadial: TTMSFNCMapsPolyline;
FNCTrack: TTMSFNCMapsPolyline;
FNCPrediction: TTMSFNCMapsPolyline;
FNCHorizon: Array[0..1] of TTMSFNCMapsCircle;
Position: THABPosition;
end;
type
TfrmMap = class(TfrmNormal)
tmrCentreAtHome: TTimer;
procedure FormCreate(Sender: TObject);
procedure GMapMarkerClick(Sender: TObject; MarkerTitle: string;
IdMarker: Integer; Latitude, Longitude: Double; Button: TMouseButton);
procedure FNCMapMapInitialized(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure tmrCentreAtHomeTimer(Sender: TObject);
private
{ Private declarations }
NeedToHome, CanHome: Boolean;
// MarkerNames: array[0..64] of String;
// PolylineItems: array[0..64] of TPolylineItem;
FollowMode: TFollowMode;
// function FindMapMarker(PayloadID: String): Integer;
FNCMap: TTMSFNCMaps;
procedure DrawBalloonRadials;
protected
ImageFolder: String;
Balloons: Array[1..MAX_PAYLOADS] of TBalloon;
FNCMastMarker: TTMSFNCMapsMarker;
procedure DrawPath(PayloadIndex: Integer; Position: THABPosition; Colour: TColor);
procedure CreateMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
procedure UpdateMapMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
procedure SetMapHomePosition;
procedure DrawRadial(PayloadIndex: Integer; NewHome: Boolean);
procedure DrawHorizon(PayloadIndex: Integer);
procedure RemoveMapPayload(PayloadIndex: Integer);
procedure AddOrUpdateMapMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String); virtual;
function BalloonIconName(Position: THABPosition; ColourText: String; Target: Boolean=False): String;
public
{ Public declarations }
function ProcessNewPosition(PayloadIndex: Integer; Position: THABPosition; Colour: TColor; ColourText: String): Boolean;
procedure SetHomePosition;
procedure RemovePayload(PayloadIndex: Integer);
procedure CentrePayloadOnMap(PayloadIndex: Integer);
procedure SetLandingMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
procedure ClearPredictionPath(PayloadIndex: Integer; Colour: TColor); virtual;
procedure AppendToPredictionPath(PayloadIndex: Integer; PredictedLatitude, PredictedLongitude, PredictedAltitude: Double); virtual;
procedure SetMapSource(Source: String);
end;
var
frmMap: TfrmMap;
implementation
{$R *.dfm}
uses Main, Data, BaseTypes, Payloads;
// IF THE FOLLOWING LINE GIVES AN ERROR
{$INCLUDE 'key.pas'}
(* THEN CREATE A FILE key.pas CONTAINING:
const GoogleMapsAPIKey = '<YOUR_GOOGLE_MAPS_API_KEY>';
const BingMapsAPIKey = '<YOUR_BING_MAPS_API_KEY>';
const TomTomMapsAPIKey = '<YOUR_TOMTOM_MAPS_API_KEY>';
*)
// THE key.pas FILE IS SPECIFICALLY EXCLUDED in .gitignore TO AVOID SHARING API KEYS
procedure TfrmMap.FNCMapMapInitialized(Sender: TObject);
begin
CanHome := True;
end;
procedure TfrmMap.FormCreate(Sender: TObject);
var
Path: String;
begin
FollowMode := fmInit;
FNCMap := frmMain.FNCMap;
if GetCommandLineParameter('PATH', Path) then begin
ImageFolder := Path + '\Images\';
end else begin
ImageFolder := ExtractFilePath(Application.ExeName) + 'Images\';
end;
SetHomePosition;
SetMapSource('LEAFLET');
end;
procedure TfrmMap.FormShow(Sender: TObject);
begin
tmrCentreAtHome.Enabled := True;
end;
procedure TfrmMap.SetMapHomePosition;
var
FileName: String;
PayloadIndex: Integer;
begin
if FNCMastMarker = nil then begin
FNCMastMarker := FNCMap.Markers.Add;
FNCMastMarker.Latitude := DataModule1.tblSettings.FieldByName('Latitude').Asfloat;
FNCMastMarker.Longitude := DataModule1.tblSettings.FieldByName('Longitude').Asfloat;
FNCMastMarker.Title := 'Home';
FileName := ImageFolder + 'mast.png';
if FileExists(FileName) then begin
FNCMastMarker.IconURL := StringReplace('File://' + FileName, '\', '/',[rfReplaceAll, rfIgnoreCase]);
end;
end else begin
FNCMastMarker.Latitude := DataModule1.tblSettings.FieldByName('Latitude').Asfloat;
FNCMastMarker.Longitude := DataModule1.tblSettings.FieldByName('Longitude').Asfloat;
end;
NeedToHome := True;
end;
procedure TfrmMap.DrawBalloonRadials;
var
PayloadIndex: Integer;
begin
for PayloadIndex := Low(Balloons) to High(Balloons) do begin
if Balloons[PayloadIndex].InUse then begin
DrawRadial(PayloadIndex, True);
end;
end;
end;
procedure TfrmMap.GMapMarkerClick(Sender: TObject; MarkerTitle: string;
IdMarker: Integer; Latitude, Longitude: Double; Button: TMouseButton);
begin
frmPayloads.HighlightPayload(MarkerTitle, True, False);
end;
function TfrmMap.BalloonIconName(Position: THABPosition; ColourText: String; Target: Boolean=False): String;
begin
if Position.IsChase then begin
Result := 'car-' + ColourText;
end else begin
Result := 'balloon-' + ColourText;
// if Target then begin
// Result := 'x-' + BalloonColour;
// end else begin
case Position.FlightMode of
fmIdle: Result := 'balloon-' + ColourText;
fmLaunched: Result := 'balloon-' + ColourText;
fmDescending: Result := 'parachute-' + ColourText;
fmLanded: Result := 'payload-' + ColourText;
end;
end;
end;
function TfrmMap.ProcessNewPosition(PayloadIndex: Integer; Position: THABPosition; Colour: TColor; ColourText: String): Boolean;
begin
Result := False;
if (Position.Latitude <> 0) and (Position.Longitude <> 0) then begin
Balloons[PayloadIndex].Position := Position;
FNCMap.BeginUpdate;
// Find or create marker for this payload
AddOrUpdateMapMarker(PayloadIndex, Position, ColourText);
if not Position.IsChase then begin
// Line to Balloon
DrawRadial(PayloadIndex, False);
// Reception horizon
DrawHorizon(PayloadIndex);
end;
// Path
DrawPath(PayloadIndex, Position, Colour);
// Pan to balloon
// if (FollowMode = fmPayload) and (Index = SelectedIndex) then begin
// GMap.MapPanTo(Positions[Index].Position.Latitude, Positions[Index].Position.Longitude);
// end;
FNCMap.EndUpdate;
Result := True;
end;
end;
procedure TfrmMap.AddOrUpdateMapMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
begin
if not Balloons[PayloadIndex].InUse then begin
Balloons[PayloadIndex].InUse := True;
CreateMarker(PayloadIndex, Position, ColourText);
end else if Balloons[PayloadIndex].FNCMarker <> nil then begin
Balloons[PayloadIndex].FNCMarker.Latitude := Position.Latitude;
Balloons[PayloadIndex].FNCMarker.Longitude := Position.Longitude;
end;
if Position.PredictionType <> ptNone then begin
SetLandingMarker(PayloadIndex, Position, ColourText);
end;
UpdateMapMarker(PayloadIndex, Position, ColourText);
end;
procedure TfrmMap.SetHomePosition;
begin
if (not DataModule1.tblSettings.FieldByName('Latitude').IsNull) and (not DataModule1.tblSettings.FieldByName('Longitude').IsNull) then begin
SetMapHomePosition;
DrawBalloonRadials;
end;
end;
procedure TfrmMap.RemovePayload(PayloadIndex: Integer);
begin
with Balloons[PayloadIndex] do begin
InUse := False;
RemoveMapPayload(PayloadIndex);
end;
end;
//function TfrmMap.FindMapMarker(PayloadID: String): Integer;
//var
// i: Integer;
//begin
// for i := 0 to GMap.Markers.Count-1 do begin
// if GMap.Markers[i].Title = PayloadID then begin
// // Found it
// Result := i;
// Exit;
// end;
// end;
//
// Result := -1;
//end;
procedure TfrmMap.DrawPath(PayloadIndex: Integer; Position: THABPosition; Colour: TColor);
begin
if Balloons[PayloadIndex].FNCTrack = nil then begin
Balloons[PayloadIndex].FNCTrack := FNCMap.Polylines.Add;
Balloons[PayloadIndex].FNCTrack.StrokeColor := Colour;
// FNCMap.CreateMapPolyline(Balloons[PayloadIndex].FNCTrack.Polyline);
end else begin
// Balloons[PayloadIndex].Track.Polyline.Path.Add(Position.Latitude, Position.Longitude);
// FNCMap.UpdateMapPolyline(Balloons[PayloadIndex].FNCTrack.Polyline);
end;
Balloons[PayloadIndex].FNCTrack.Visible := False;
Balloons[PayloadIndex].FNCTrack.Coordinates.Add;
Balloons[PayloadIndex].FNCTrack.Coordinates.Items[Balloons[PayloadIndex].FNCTrack.Coordinates.Count-1].Latitude := Position.Latitude;
Balloons[PayloadIndex].FNCTrack.Coordinates.Items[Balloons[PayloadIndex].FNCTrack.Coordinates.Count-1].Longitude := Position.Longitude;
Balloons[PayloadIndex].FNCTrack.Visible := True;
end;
procedure TfrmMap.CreateMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
begin
// inherited
Balloons[PayloadIndex].FNCMarker := FNCMap.Markers.Add;
Balloons[PayloadIndex].FNCMarker.Latitude := Position.Latitude;
Balloons[PayloadIndex].FNCMarker.Longitude := Position.Longitude;
Balloons[PayloadIndex].FNCMarker.Title := Position.PayloadID;
// if Position.IsSonde then begin
// Balloons[PayloadIndex].FNCMarker.MapLabel.Color := clYellow;
// end else begin
// Balloons[PayloadIndex].FNCMarker.MapLabel.Color := clLime;
// end;
//
// Balloons[PayloadIndex].FNCMarker.MapLabel.Text := Position.PayloadID;
// Balloons[PayloadIndex].FNCMarker.MapLabel.OffsetTop := 70;
end;
procedure TfrmMap.SetLandingMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
var
FileName: String;
begin
if Balloons[PayloadIndex].FNCLanding = nil then begin
FileName := ImageFolder + 'x-' + ColourText + '.png';
Balloons[PayloadIndex].FNCLanding := FNCMap.Markers.Add;
Balloons[PayloadIndex].FNCLanding.Title := Position.PayloadID;
Balloons[PayloadIndex].FNCLanding.IconURL := StringReplace('File://' + FileName, '\', '/',[rfReplaceAll, rfIgnoreCase]);
end;
Balloons[PayloadIndex].FNCLanding.Latitude := Position.PredictedLatitude;
Balloons[PayloadIndex].FNCLanding.Longitude := Position.PredictedLongitude;
end;
procedure TfrmMap.DrawRadial(PayloadIndex: Integer; NewHome: Boolean);
begin
if (not DataModule1.tblSettings.FieldByName('Latitude').IsNull) and (not DataModule1.tblSettings.FieldByName('Longitude').IsNull) then begin
if Balloons[PayloadIndex].FNCRadial = nil then begin
Balloons[PayloadIndex].FNCRadial := FNCMap.Polylines.Add;
Balloons[PayloadIndex].FNCRadial.StrokeColor := clLime;
// Add ends of line
Balloons[PayloadIndex].FNCRadial.Coordinates.Add;
Balloons[PayloadIndex].FNCRadial.Coordinates.Add;
NewHome := True;
end;
if NewHome then begin
// Start at home
Balloons[PayloadIndex].FNCRadial.Coordinates[0].Latitude := DataModule1.tblSettings.FieldByName('Latitude').AsFloat;
Balloons[PayloadIndex].FNCRadial.Coordinates[0].Longitude := DataModule1.tblSettings.FieldByName('Longitude').AsFloat;
end;
// Target at balloon
Balloons[PayloadIndex].FNCRadial.Visible := False;
Balloons[PayloadIndex].FNCRadial.Coordinates[1].Latitude := Balloons[PayloadIndex].Position.Latitude;
Balloons[PayloadIndex].FNCRadial.Coordinates[1].Longitude := Balloons[PayloadIndex].Position.Longitude;
Balloons[PayloadIndex].FNCRadial.Visible := True;
end;
end;
procedure TfrmMap.DrawHorizon(PayloadIndex: Integer);
const
Colors: Array[0..1] of TColor = (clBlue, clLime);
Radius: Array[0..1] of Double = (0.0, 5.0);
var
i: Integer;
NewCircle: Boolean;
begin
for i := 0 to 1 do begin
if Balloons[PayloadIndex].FNCHorizon[i] = nil then begin
Balloons[PayloadIndex].FNCHorizon[i] := FNCMap.Circles.Add;
Balloons[PayloadIndex].FNCHorizon[i].StrokeColor := Colors[i];
Balloons[PayloadIndex].FNCHorizon[i].FillOpacity := 0;
// Balloons[PayloadIndex].FNCHorizon[i].Polygon.HoverBorderColor := Colors[i];
// Balloons[PayloadIndex].FNCHorizon[i].Polygon.HoverBackgroundColor := clNone;
// Balloons[PayloadIndex].FNCHorizon[i].Polygon.BackgroundOpacity := 0;
Balloons[PayloadIndex].FNCHorizon[i].StrokeWidth := 2;
NewCircle := True;
end else begin;
NewCircle := False;
end;
Balloons[PayloadIndex].FNCHorizon[i].Radius := Round(1000.0 * CalculateHorizonRadius(Balloons[PayloadIndex].Position.Altitude, Radius[i]));
Balloons[PayloadIndex].FNCHorizon[i].Center.Latitude := Balloons[PayloadIndex].Position.Latitude;
Balloons[PayloadIndex].FNCHorizon[i].Center.Longitude := Balloons[PayloadIndex].Position.Longitude;
// if NewCircle then begin
// GMap.CreateMapPolygon(Balloons[PayloadIndex].Horizon[i].Polygon);
// end else begin
// GMap.UpdateMapPolygon(Balloons[PayloadIndex].Horizon[i].Polygon);
// end;
end;
end;
procedure TfrmMap.RemoveMapPayload(PayloadIndex: Integer);
begin
with Balloons[PayloadIndex] do begin
if FNCMarker <> nil then begin
FNCMarker.Free;
FNCMarker := nil;
end;
if FNCLanding <> nil then begin
FNCLanding.Free;
FNCLanding := nil;
end;
if FNCRadial <> nil then begin
FNCRadial.Free;
FNCRadial := nil;
end;
if FNCTrack <> nil then begin
FNCTrack.Coordinates.Clear;
end;
if FNCHorizon[0] <> nil then begin
FNCHorizon[0].Free;
FNCHorizon[0] := nil;
end;
if FNCHorizon[1] <> nil then begin
FNCHorizon[1].Free;
FNCHorizon[1] := nil;
end;
end;
end;
procedure TfrmMap.CentrePayloadOnMap(PayloadIndex: Integer);
begin
if PayloadIndex >= 0 then begin
if Balloons[PayloadIndex].FNCMarker <> nil then begin
FNCMap.SetCenterCoordinate(Balloons[PayloadIndex].FNCMarker.Latitude, Balloons[PayloadIndex].FNCMarker.Longitude);
end;
end else begin
SetHomePosition;
// FNCMap.SetCenterCoordinate(FNCMap.Options.DefaultLatitude, FNCMap.Options.DefaultLongitude);
end;
end;
procedure TfrmMap.UpdateMapMarker(PayloadIndex: Integer; Position: THABPosition; ColourText: String);
var
FileName: String;
begin
if Balloons[PayloadIndex].FNCMarker <> nil then begin
FileName := ImageFolder + BalloonIconName(Position, ColourText, False) + '.png';
if FileName <> Balloons[PayloadIndex].MarkerName then begin
Balloons[PayloadIndex].MarkerName := FileName;
if FileExists(FileName) then begin
Balloons[PayloadIndex].FNCMarker.IconURL := StringReplace('File://' + FileName, '\', '/',[rfReplaceAll, rfIgnoreCase]);
end;
end;
end;
end;
procedure TfrmMap.ClearPredictionPath(PayloadIndex: Integer; Colour: TColor);
begin
// virtual
end;
procedure TfrmMap.AppendToPredictionPath(PayloadIndex: Integer; PredictedLatitude, PredictedLongitude, PredictedAltitude: Double);
begin
// virtual
end;
procedure TfrmMap.SetMapSource(Source: String);
begin
Source := UpperCase(Source);
FNCMap.APIKey := '';
CanHome := False;
if Source = 'AZURE' then begin
FNCMap.Service := msAzureMaps;
end else if Source = 'BING' then begin
FNCMap.APIKey := BingMapsAPIKey;
FNCMap.Service := msBingMaps;
end else if Source = 'GOOGLE' then begin
FNCMap.APIKey := GoogleMapsAPIKey;
FNCMap.Service := msGoogleMaps;
end else if Source = 'HERE' then begin
FNCMap.Service := msHere;
end else if Source = 'LEAFLET' then begin
FNCMap.Service := msLeaflet;
end else if Source = 'MAPBOX' then begin
FNCMap.Service := msMapBox;
end else if Source = 'MAPKIT' then begin
FNCMap.Service := msMapKit;
end else if Source = 'OPEN' then begin
FNCMap.Service := msOpenLayers;
end else if Source = 'TOMTOM' then begin
FNCMap.APIKey := TomTomMapsAPIKey;
FNCMap.Service := msTomTom;
end;
NeedToHome := True;
end;
procedure TfrmMap.tmrCentreAtHomeTimer(Sender: TObject);
begin
if NeedToHome and CanHome then begin
NeedToHome := False;
FNCMap.SetCenterCoordinate(DataModule1.tblSettings.FieldByName('Latitude').Asfloat, DataModule1.tblSettings.FieldByName('Longitude').AsFloat);
end;
end;
end.