forked from cleyjulio/maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.example.pas
394 lines (274 loc) · 9.85 KB
/
map.example.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
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
unit map.example;
interface
uses
System.Types, System.UITypes, System.JSON, System.SysUtils, System.Math, System.Generics.Collections, System.Sensors.Components, System.Classes,
System.Sensors,
FMX.Maps, FMX.StdCtrls, FMX.Controls, FMX.Forms, FMX.Objects,
FireDAC.Comp.Client, FireDAC.Stan.Param;
Type TMapExample = class
type TTesterecord = Record
End;
TPolyArray = TArray<TMapPolygon>;
TMarkersArray = Array of TMapMarker;
TLineArray = Array of TMapPolyline;
private
class function CenterPolyline(arrPoints: TArray<TMapCoordinate>): TMapCoordinate;
class procedure MkrName(coords: TMapCoordinate; Name: String; Map: TMapView; RecNameTh: TRectangle; lblNameTh: TLabel; var Names: TMarkersArray);
class procedure LineDistance(coord1, coord2: TMapCoordinate; Map: TMapView; var aList: TLineArray);
class function ReadJson(number: smallint): TJSONObject;
public
class function SetSensor(Sensor: TLocationSensor): TLocationSensor;
class procedure ChangeMarker(Mkr: TMapMarker; Map: TMapView; RecNameTh: TRectangle; lblNameTh: TLabel; var Names: TMarkersArray);
class function RemovePolygons(var aList: TPolyArray): TPolyArray;
class function RemoveMarkers(var mList: TMarkersArray): TMarkersArray;
class procedure SetVisibleMarkers(mList: TMarkersArray; Visible: Boolean);
class procedure SetVisibleLine(mList: TLineArray; Visible: Boolean);
class function RemoveLine(aList: TLineArray): TLineArray;
class procedure Distance(LookAt, Position: TMapCoordinate; Map: TMapView; lblDistancia: TLabel; var aList: TLineArray); overload;
class procedure ShowPolygon(LookAt, Position: TMapCoordinate; var arrPoints: TArray<TMapCoordinate>; Map: TMapView;
RecNameTh: TRectangle; LblDistance, lblNameTh: TLabel; var arrPols: TArray<TMapPolygon>; var aList: array of TMapPolyline;
var Names: TMapExample.TMarkersArray);
class procedure BuildPolygon(var Map: TMapView; RecNameTh: TRectangle; LblDistance, lblNameTh: TLabel;
Name: String; LookAt, Position: TMapCoordinate; var arrPols: TPolyArray;
var arrPoints: TArray<TMapCoordinate>; var aList: array of TMapPolyline;
var Names: TMarkersArray);
end;
implementation
uses System.IOUtils;
{ TMapsttivos }
class procedure TMapExample.BuildPolygon(var Map: TMapView; RecNameTh: TRectangle; LblDistance, lblNameTh: TLabel; Name: String; LookAt,
Position: TMapCoordinate; var arrPols: TPolyArray; var arrPoints: TArray<TMapCoordinate>; var aList: array of TMapPolyline;
var Names: TMarkersArray);
var descPolygon: TMapPolygonDescriptor;
begin
try
Map.BeginUpdate;
{polygon (utiliza os pontos lidos do Json)}
descPolygon := TMapPolygonDescriptor.Create(arrPoints);
{Caracteristicas}
descPolygon.StrokeColor := TAlphaColor($96FFA500);
descPolygon.StrokeWidth := 6;
descPolygon.Geodesic := false;
descPolygon.ZIndex := 0.0;
descPolygon.fillcolor := TAlphaColor($96FFA500);
{Adicionar ao mapa}
arrPols := arrPols + [Map.AddPolygon(descPolygon)];
{centralizar o mapa (calculado aprox. a partir dos pontos)}
LookAt := CenterPolyline(arrPoints);
Map.Location := LookAt;
Map.Zoom := 13;
{Adiciona os nomes da áreas aos poligonos na forma marcadores}
MkrName(LookAt, Name, Map, RecNameTh, lblNameTh, Names);
Map.EndUpdate;
except
Map.Visible := false;
end;
end;
class function TMapExample.CenterPolyline(arrPoints: TArray<TMapCoordinate>): TMapCoordinate;
var
tLat, tLon: double;
i, j: integer;
begin
i := Length(arrPoints);
tLat := 0;
tLon := 0;
for j := Low(arrPoints) to High(arrPoints) do
begin
tLat := tLat + arrPoints[j].Latitude;
tLon := tLon + arrPoints[j].Longitude;
end;
Result.Latitude := tLat / i;
Result.Longitude := tLon / i;
end;
class procedure TMapExample.ChangeMarker(Mkr: TMapMarker; Map: TMapView; RecNameTh: TRectangle; lblNameTh: TLabel; var Names: TMarkersArray);
var Mkr_New: TMapMarkerDescriptor;
i: smallint;
s: string;
begin
Map.BeginUpdate;
Mkr_New := Mkr.Descriptor;
Mkr.Remove;
i := Pos('S', Mkr_New.Snippet);
if i > 0 then
begin
s := Mkr_New.Snippet;
RecNameTh.Fill.color := TAlphaColorRec.White;
Delete(s, i, 1);
Mkr_New.Snippet := s;
end
else
begin
RecNameTh.Fill.color := TAlphaColorRec.Yellow;
Mkr_New.Snippet := Mkr_New.Snippet + 'S';
end;
lblNameTh.Text := Mkr.Descriptor.Title;
Mkr_New.Icon := RecNameTh.MakeScreenshot;
Names := Names + [Map.AddMarker(Mkr_New)];
Map.EndUpdate;
end;
class function TMapExample.ReadJson(number: smallint): TJSONObject;
var arq: TStringList;
s: String;
begin
//Ler JSON KML
arq := TStringList.Create;
arq.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'th' + number.ToString + '.json');
s := arq.Text;
Result := TJSONObject.ParseJSONValue(s) as TJSONObject;
arq.Free;
end;
class function TMapExample.RemoveLine(aList: TLineArray): TLineArray;
var i: integer;
begin
if High(aList) > -1 then
for i := 0 to High(aList) do
if Assigned(aList[i]) then
aList[i].Remove;
result := aList;
end;
class procedure TMapExample.Distance(LookAt, Position: TMapCoordinate; Map: TMapView; lblDistancia: TLabel; var aList: TLineArray);
const
diameter = 2 * 6371.0;
var
dx, dy, dz, r, long1, lat1, lat2: double;
unidade: string;
begin
long1 := degtorad(LookAt.Longitude - Position.Longitude);
lat1 := degtorad(LookAt.Latitude);
lat2 := degtorad(Position.Latitude);
dz := sin(lat1) - sin(lat2);
dx := cos(long1) * cos(lat1) - cos(lat2);
dy := sin(long1) * cos(lat1);
r := (arcsin(sqrt(sqr(dx) + sqr(dy) + sqr(dz)) / 2) * diameter) * 1000;
if r >= 1000 then
begin
r := r / 1000;
unidade := ' Km';
end
else
unidade := ' m';
lblDistancia.Text := FormatFloat('#,##0.00', r) + unidade;
LineDistance(LookAt, Position, Map, aList);
end;
class procedure TMapExample.LineDistance(coord1, coord2: TMapCoordinate; Map: TMapView; var aList: TLineArray);
var AB : TMapPolylineDescriptor;
begin
AB := TMapPolylineDescriptor.Create([coord1, coord2]);
AB.StrokeColor := TAlphaColorRec.Blue;
AB.Geodesic := True;
AB.StrokeWidth := 5;
SetLength(aList, Length(aList) + 1);
aList[high(aList)] := Map.AddPolyline(AB);
end;
class procedure TMapExample.MkrName(coords: TMapCoordinate; Name: String; Map: TMapView; RecNameTh: TRectangle;
lblNameTh: TLabel; var Names: TMarkersArray);
var MyMarker : TMapMarkerDescriptor;
begin
Map.BeginUpdate;
Map.Visible := True;
Map.Location := TMapCoordinate(coords);
MyMarker := TMapMarkerDescriptor.Create(coords, Name);
MyMarker.Title := Name;
MyMarker.Draggable := false;
MyMarker.Visible := True;
lblNameTh.Text := Name;
RecNameTh.Fill.color := TAlphaColorRec.White;
MyMarker.Icon := RecNameTh.MakeScreenshot;
Names := Names + [Map.AddMarker(MyMarker)];
Map.EndUpdate;
end;
class function TMapExample.RemoveMarkers(var mList: TMarkersArray): TMarkersArray;
var i: integer;
begin
try
for i := 0 to Pred(Length(mList)) do
begin
mList[i].Remove;
mList[i] := nil;
end;
except
end;
Result := mList;
end;
class function TMapExample.RemovePolygons(var aList: TPolyArray): TPolyArray;
var i: integer;
begin
Result := aList;
try
for i := 0 to Pred(Length(aList)) do
begin
aList[i].Remove;
aList[i].Free;
aList[i] := nil;
end;
except
end;
Result := aList;
end;
class function TMapExample.SetSensor(Sensor: TLocationSensor): TLocationSensor;
begin
Sensor.Accuracy := 0.0001;
Sensor.Distance := 0.0001;
Sensor.ActivityType := TLocationActivityType.Automotive;
Sensor.LocationChange := TLocationChangeType.lctSmall;
Sensor.Optimize := True;
Sensor.UsageAuthorization := TLocationUsageAuthorization.WhenInUse;
Sensor.Active := True;
Result := Sensor;
end;
class procedure TMapExample.SetVisibleLine(mList: TLineArray; Visible: Boolean);
var i: integer;
begin
try
for i := 0 to Pred(Length(mList)) do
mList[i].SetVisible(Visible);
except
end;
end;
class procedure TMapExample.SetVisibleMarkers(mList: TMarkersArray; Visible: Boolean);
var i: integer;
begin
try
for i := 0 to Pred(Length(mList)) do
mList[i].SetVisible(Visible);
except
end;
end;
class procedure TMapExample.ShowPolygon(LookAt, Position: TMapCoordinate; var arrPoints: TArray<TMapCoordinate>; Map: TMapView;
RecNameTh: TRectangle; LblDistance, lblNameTh: TLabel; var arrPols: TArray<TMapPolygon>; var aList: array of TMapPolyline;
var Names: TMapExample.TMarkersArray);
var Kml, Coordenada, Look: TJSONObject;
Coordenadas: TJsonArray;
i, j: integer;
name: String;
begin
Kml := nil;
try
for i := 1 to 2 do
begin
//aqui lê os dois arquivos json de exemplo contendo as coordenadas.
Kml := ReadJson(i);
if Assigned(Kml) then
begin
Look := (Kml.GetValue('kml') As TJSONObject).GetValue('lookAt') As TJSONObject;
LookAt.Latitude := StringReplace(Look.GetValue('latitude').Value, '.', ',', [rfReplaceAll]).ToDouble;
LookAt.Longitude := StringReplace(Look.GetValue('longitude').Value, '.', ',', [rfReplaceAll]).ToDouble;
Name := (Kml.GetValue('kml') As TJSONObject).GetValue('nome').ToString;
Coordenadas := (Kml.GetValue('kml') As TJSONObject).GetValue('coordenadas') As TJsonArray;
SetLength(arrPoints, Coordenadas.Count);
for j := 0 to Coordenadas.Count - 1 do
begin
Coordenada := Coordenadas.Items[j] As TJSONObject;
arrPoints[j].Latitude := StringReplace(Coordenada.GetValue('latitude').Value, '.', ',', [rfReplaceAll]).ToDouble;
arrPoints[j].Longitude := StringReplace(Coordenada.GetValue('longitude').Value, '.', ',', [rfReplaceAll]).ToDouble;
end;
//chama a função para gerar os polígonos...
BuildPolygon(Map, RecNameTh, LblDistance, lblNameth, Name, LookAt, Position, arrPols, arrPoints, aList, Names);
end;
end;
Kml.Free;
except
Kml.Free;
end;
end;
end.