-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMap.pas
More file actions
252 lines (205 loc) · 7.71 KB
/
Copy pathMap.pas
File metadata and controls
252 lines (205 loc) · 7.71 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
unit Map;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
Base, TargetForm, System.IOUtils, FMX.Controls.Presentation,
Source, FMX.Objects, FMX.TMSFNCTypes, FMX.TMSFNCUtils,
FMX.TMSFNCGraphics, FMX.TMSFNCGraphicsTypes, FMX.TMSFNCMapsCommonTypes,
FMX.TMSFNCCustomControl, FMX.TMSFNCWebBrowser, FMX.TMSFNCMaps;
type
TMapFollowMode = (mmIdle, mmInit, mmHome, mmFree, mmCar, mmPayload);
type
TDirections = record
Active: Boolean;
TargetLatitude: Double;
TargetLongitude: Double;
CurrentLatitude: Double;
CurrentLongitude: Double;
end;
type
TMapPayload = record
Markers: Array[0..1] of TTMSFNCMapsMarker;
MarkerNames: Array[0..1] of String;
Track: TTMSFNCMapsPolyline;
end;
type
TfrmMap = class(TfrmTarget)
pnlCentre: TPanel;
tmrDirections: TTimer;
procedure FormCreate(Sender: TObject);
procedure btnCarClick(Sender: TObject);
procedure btnFreeClick(Sender: TObject);
procedure btnPayloadClick(Sender: TObject);
procedure tmrDirectionsTimer(Sender: TObject);
private
{ Private declarations }
Directions: TDirections;
FollowMode: TMapFollowMode;
Balloons: Array[0..3] of TMapPayload;
procedure AddOrUpdateMapMarker(PayloadIndex: Integer; PayloadID: String; Latitude: Double; Longitude: Double; ImageName: String; IsTarget: Boolean);
procedure DrawPath(PayloadIndex: Integer; Colour: TAlphaColor);
protected
function ProcessNewPosition(Index: Integer): Boolean; override;
public
{ Public declarations }
procedure LoadForm; override;
procedure HideForm; override;
procedure ShowDirections(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude: Double);
procedure MapInitialized;
end;
var
frmMap: TfrmMap;
implementation
{$R *.fmx}
uses Main, Miscellaneous, Debug;
// IF THE FOLLOWING LINE GIVES AN ERROR
{$INCLUDE 'key.pas'}
(* THEN CREATE A FILE key.pas CONTAINING:
const GoogleMapsAPIKey = '<YOUR_GOOGLE_API_KEY>';
THIS FILE IS SPECIFICALLY EXCLUDED in .gitignore TO AVOID SHARING API KEYS
*)
procedure TfrmMap.FormCreate(Sender: TObject);
var
i: Integer;
begin
inherited;
frmMain.FNCMap.APIKey := GoogleMapsAPIKey;
// GMap.LocationAPIKey := GoogleMapsAPIKey;
//
// GMap.APIKey := GMap.LocationAPIKey;
//
// // GMap.ShowDebugConsole := True;
//
// {$IFDEF ANDROID}
// GMap.Visible := False;
// {$ENDIF}
//
// for i := 0 to 3 do begin
// PolylineItems[i] := GMap.Polylines.Add;
// PolylineItems[i].Polyline.Width := 2;
// GMap.CreateMapPolyline(PolylineItems[i].Polyline);
// end;
//
// FollowMode := fmInit;
//
// OKToUpdateMap := True;
end;
procedure TfrmMap.LoadForm;
begin
inherited;
// if not GMap.Visible then begin
// GMap.Visible := True;
// end;
// btnCar.Font.Size := btnCar.Size.Height * 36/64;
// btnFree.Font.Size := btnCar.Font.Size;
// btnPayload.Font.Size := btnCar.Font.Size;
end;
procedure TfrmMap. MapInitialized;
begin
if FollowMode = mmInit then begin
if Positions[0].Position.InUse then begin
frmMain.FNCMap.SetCenterCoordinate(Positions[0].Position.Latitude, Positions[0].Position.Longitude);
btnFreeClick(nil);
end;
end;
end;
procedure TfrmMap.HideForm;
begin
// {$IFDEF ANDROID}
// GMap.Visible := False;
// {$ENDIF}
inherited;
end;
procedure TfrmMap.btnCarClick(Sender: TObject);
begin
FollowMode := mmCar;
frmMain.FNCMap.SetCenterCoordinate(Positions[0].Position.Latitude, Positions[0].Position.Longitude);
end;
procedure TfrmMap.btnFreeClick(Sender: TObject);
begin
FollowMode := mmFree;
end;
procedure TfrmMap.btnPayloadClick(Sender: TObject);
begin
FollowMode := mmPayload;
if Positions[SelectedIndex].Position.InUse then begin
frmMain.FNCMap.SetCenterCoordinate(Positions[SelectedIndex].Position.Latitude, Positions[SelectedIndex].Position.Longitude);
end;
end;
procedure TfrmMap.AddOrUpdateMapMarker(PayloadIndex: Integer; PayloadID: String; Latitude: Double; Longitude: Double; ImageName: String; IsTarget: Boolean);
var
MarkerIndex: Integer;
FileName: String;
begin
if IsTarget then MarkerIndex := 1 else MarkerIndex := 0;
if Balloons[PayloadIndex].Markers[MarkerIndex] = nil then begin
Balloons[PayloadIndex].Markers[MarkerIndex] := frmMain.FNCMap.Markers.Add;
Balloons[PayloadIndex].Markers[MarkerIndex].Title := PayloadID;
end;
Balloons[PayloadIndex].Markers[MarkerIndex].Latitude := Latitude;
Balloons[PayloadIndex].Markers[MarkerIndex].Longitude := Longitude;
FileName := System.IOUtils.TPath.Combine(ImageFolder, ImageName + '.png');
if FileName <> Balloons[PayloadIndex].MarkerNames[MarkerIndex] then begin
Balloons[PayloadIndex].MarkerNames[MarkerIndex] := FileName;
if FileExists(FileName) then begin
Balloons[PayloadIndex].Markers[MarkerIndex].IconURL := StringReplace('File://' + FileName, '\', '/',[rfReplaceAll, rfIgnoreCase]);
end;
end;
end;
procedure TfrmMap.DrawPath(PayloadIndex: Integer; Colour: TAlphaColor);
begin
if Balloons[PayloadIndex].Track = nil then begin
Balloons[PayloadIndex].Track := frmMain.FNCMap.Polylines.Add;
Balloons[PayloadIndex].Track.StrokeColor := Colour;
end;
with Balloons[PayloadIndex].Track.Coordinates.Add do begin
Latitude := Positions[PayloadIndex].Position.Latitude;
Longitude := Positions[PayloadIndex].Position.Longitude;
end;
Balloons[PayloadIndex].Track.Visible := False;
Balloons[PayloadIndex].Track.Visible := True;
end;
function TfrmMap.ProcessNewPosition(Index: Integer): Boolean;
begin
Result := False;
// Find or create marker for this payload
if Index = 0 then begin
// Update chase car marker
AddOrUpdateMapMarker(Index, 'Car', Positions[0].Position.Latitude, Positions[0].Position.Longitude, 'car-blue', False);
if FollowMode = mmCar then begin
frmMain.FNCMap.SetCenterCoordinate(Positions[Index].Position.Latitude, Positions[Index].Position.Longitude);
end;
end else begin
// Update balloon marker
AddOrUpdateMapMarker(Index,
Positions[Index].Position.PayloadID,
Positions[Index].Position.Latitude,
Positions[Index].Position.Longitude,
frmMain.BalloonIconName(Index, False), False);
DrawPath(Index, frmMain.BalloonColour(Index));
if (FollowMode = mmPayload) and (Index = SelectedIndex) then begin
frmMain.FNCMap.SetCenterCoordinate(Positions[Index].Position.Latitude, Positions[Index].Position.Longitude);
end;
if Positions[Index].Position.PredictionType <> ptNone then begin
AddOrUpdateMapMarker(Index, Positions[Index].Position.PayloadID + '-X', Positions[Index].Position.PredictedLatitude, Positions[Index].Position.PredictedLongitude, frmMain.BalloonIconName(Index, True), True);
end;
end;
Result := True;
end;
procedure TfrmMap.tmrDirectionsTimer(Sender: TObject);
begin
tmrDirections.Enabled := False;
with Directions do begin
frmMain.FNCMap.AddDirections(CurrentLatitude, CurrentLongitude, TargetLatitude, TargetLongitude); // , tmDriving);
end;
end;
procedure TfrmMap.ShowDirections(TargetLatitude, TargetLongitude, CurrentLatitude, CurrentLongitude: Double);
begin
Directions.TargetLatitude := TargetLatitude;
Directions.TargetLongitude := TargetLongitude;
Directions.CurrentLatitude := CurrentLatitude;
Directions.CurrentLongitude := CurrentLongitude;
Directions.Active := True;
end;
end.