forked from academiadocodigo/TBGWebCharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maps.Gmaps.Routes.Directions.pas
222 lines (193 loc) · 6.75 KB
/
Maps.Gmaps.Routes.Directions.pas
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
unit Maps.Gmaps.Routes.Directions;
interface
uses
Interfaces,
Charts.Types;
type
TModelMapsGMapsRoutesDirections = class(TInterfacedObject, iModelMapsRoutesDirections)
private
[weak]
FParent: iModelMapsRoutes;
FDataSet : iModelMapsDataSet<iModelMapsRoutesDirections>;
FOrigin : string;
FDestination : string;
FWayPoints : string;
FTravelMode : string;
FOptimizeWaypoints : string;
FPanel : iModelMapsRoutesDirectionsPanel;
public
constructor Create(Parent : iModelMapsRoutes);
destructor Destroy; override;
class function New(Parent : iModelMapsRoutes) : iModelMapsRoutesDirections;
function DataSet : iModelMapsDataSet<iModelMapsRoutesDirections>;
function ResultScript(MapName: string) : String;
function Origin(Value : string) : iModelMapsRoutesDirections;
function Destination(Value : string) : iModelMapsRoutesDirections;
function TravelMode(Value : TTypeMapTravelMode) : iModelMapsRoutesDirections;
function OptimizeWaypoints(Value : boolean) : iModelMapsRoutesDirections;
function Panel : iModelMapsRoutesDirectionsPanel;
procedure GeneratedWaypoints;
function GeneratedPanel(MapName : string) : string;
function &End : iModelMapsRoutes;
end;
implementation
uses
Injection,
Maps.Dataset, Utilities.Str, Maps.Gmaps.Routes.Directions.Panel;
{ TModelMapsGMapsRoutesDirections }
constructor TModelMapsGMapsRoutesDirections.Create(Parent: iModelMapsRoutes);
begin
{$IF RTLVERSION > 27 }
TInjection.Weak(@FParent, Parent);
{$ELSE}
FParent := Parent;
{$IFEND}
FDataSet := TModelMapsDataSet<iModelMapsRoutesDirections>.new(Self);
FTravelMode := 'google.maps.TravelMode.DRIVING';
FOptimizeWaypoints := 'true';
end;
function TModelMapsGMapsRoutesDirections.DataSet: iModelMapsDataSet<iModelMapsRoutesDirections>;
begin
Result := FDataSet;
end;
function TModelMapsGMapsRoutesDirections.Destination(
Value: string): iModelMapsRoutesDirections;
begin
Result := Self;
if Value <> '' then
FDestination := '"' + Value + '"';
end;
destructor TModelMapsGMapsRoutesDirections.Destroy;
begin
inherited;
end;
function TModelMapsGMapsRoutesDirections.&End: iModelMapsRoutes;
begin
Result := FParent;
end;
function TModelMapsGMapsRoutesDirections.GeneratedPanel(
MapName: string): string;
var
PanelMap : string;
begin
Result := '';
if Assigned(FPanel) then
begin
PanelMap := 'panel' + MapName;
Result := 'const ' + PanelMap + ' = document.getElementById("right-panel' + MapName + '");' +
PanelMap + '.style.display = "block";' +
PanelMap + '.style.height = document.getElementById("' + MapName + '").style.height;' +
PanelMap + '.style.width = "' + FPanel.Width + '";' +
PanelMap + '.style.float = "' + FPanel.FloatPos + '";' +
PanelMap + '.style.overflow = "auto";' +
PanelMap + '.style.lineHeight = "30px";' +
PanelMap + '.style.paddingLeft = "10px";' +
'directionsRenderer' + MapName + '.setPanel(document.getElementById("right-panel' + MapName + '"));';
end;
end;
procedure TModelMapsGMapsRoutesDirections.GeneratedWaypoints;
var
I : Integer;
addWaypoint : boolean;
Aux, LatValue, LngValue, Waypoint : string;
begin
FWayPoints := '[';
if Assigned(FDataSet.DataSet) then
begin
Aux := ',';
FDataSet.DataSet.First;
for I := 0 to Pred(FDataSet.DataSet.RecordCount) do
begin
addWaypoint := true;
LatValue := TUtilitiesStr.FloatCurrFieldToStrValue(FDataSet.DataSet.FieldByName(FDataSet.LatName));
LngValue := TUtilitiesStr.FloatCurrFieldToStrValue(FDataSet.DataSet.FieldByName(FDataSet.LngName));
if (LatValue <> '') and (LngValue <> '') then
Waypoint := '{lat: ' + LatValue + ', lng: ' + LngValue + '}'
else
Waypoint := '"' + FDataSet.DataSet.FieldByName(FDataSet.AddressName).AsString + '"';
if FOrigin = '' then
begin
FOrigin := Waypoint;
addWaypoint := false;
end;
if I = Pred(FDataSet.DataSet.RecordCount) then
begin
Aux := '';
if FDestination = '' then
begin
FDestination := Waypoint;
addWaypoint := false;
end;
end;
if addWaypoint then
FWayPoints := FWayPoints + '{location:' + Waypoint + ', stopover: true,}' + Aux;
FDataSet.DataSet.Next;
end;
end;
FWayPoints := FWayPoints + ']';
end;
class function TModelMapsGMapsRoutesDirections.New(
Parent: iModelMapsRoutes): iModelMapsRoutesDirections;
begin
Result := Self.Create(Parent);
end;
function TModelMapsGMapsRoutesDirections.OptimizeWaypoints(
Value: boolean): iModelMapsRoutesDirections;
begin
Result := Self;
if Value then
FOptimizeWaypoints := 'true'
else
FOptimizeWaypoints := 'false';
end;
function TModelMapsGMapsRoutesDirections.Origin(
Value: string): iModelMapsRoutesDirections;
begin
Result := Self;
if Value <> '' then
FOrigin := '"' + Value + '"';
end;
function TModelMapsGMapsRoutesDirections.Panel: iModelMapsRoutesDirectionsPanel;
begin
FPanel := TModelMapsGMapsRoutesDirectionsPanel.New(Self);
Result := FPanel;
end;
function TModelMapsGMapsRoutesDirections.ResultScript(MapName: string): String;
var
directionsService, directionsRenderer, waypts, MapsPanel: string;
begin
GeneratedWaypoints;
directionsService := 'directionsService' + MapName;
directionsRenderer := 'directionsRenderer' + MapName;
waypts := 'waypts' + MapName;
MapsPanel := GeneratedPanel(MapName);
Result := 'const ' + directionsService + ' = new google.maps.DirectionsService();' +
'const ' + directionsRenderer + ' = new google.maps.DirectionsRenderer();' +
directionsRenderer + '.setMap(' + MapName + ');' +
MapsPanel +
'const ' + waypts + ' = ' + FWayPoints + ';' +
directionsService + '.route(' +
'{' +
'origin: ' + FOrigin + ',' +
'destination: ' + FDestination + ',' +
'waypoints: ' + waypts + ',' +
'optimizeWaypoints: ' + FOptimizeWaypoints + ',' +
'travelMode: ' + FTravelMode + ',' +
'},' +
'(response, status) => {' +
'if (status === "OK" && response) {' +
directionsRenderer + '.setDirections(response);' +
'directionResult' + MapName + ' = response;' +
'} else {' +
'window.alert("Directions request failed due to " + status);' +
'}' +
'}' +
');';
end;
function TModelMapsGMapsRoutesDirections.TravelMode(
Value: TTypeMapTravelMode): iModelMapsRoutesDirections;
begin
Result := Self;
FTravelMode:= 'google.maps.TravelMode.' + TTypeMapTravelMode(Value).ToString;
end;
end.