forked from academiadocodigo/TBGWebCharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maps.GMaps.Draw.Marker.pas
164 lines (144 loc) · 5.5 KB
/
Maps.GMaps.Draw.Marker.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
unit Maps.GMaps.Draw.Marker;
interface
uses
Interfaces;
type
TModelMapsGMapsDrawMarker = class(TInterfacedObject, iModelMapsDrawMarker)
private
[weak]
FParent : iModelMapsDraw;
FDataSet : iModelMapsDataSet<iModelMapsDrawMarker>;
FInfoWindow : iModelMapsInfoWindow<iModelMapsDrawMarker>;
public
constructor Create(Parent : iModelMapsDraw);
destructor Destroy; override;
class function New(Parent : iModelMapsDraw) : iModelMapsDrawMarker;
function DataSet : iModelMapsDataSet<iModelMapsDrawMarker>;
function InfoWindow : iModelMapsInfoWindow<iModelMapsDrawMarker>;
function ResultScript(MapName: string) : String;
function &End : iModelMapsDraw;
end;
implementation
uses
Injection,
Maps.DataSet,
Maps.InfoWindow,
Utilities.Str;
{ TModelMapsGMapsDrawMarker }
constructor TModelMapsGMapsDrawMarker.Create(Parent: iModelMapsDraw);
begin
{$IF RTLVERSION > 27 }
TInjection.Weak(@FParent, Parent);
{$ELSE}
FParent := Parent;
{$IFEND}
FDataSet := TModelMapsDataSet<iModelMapsDrawMarker>.new(Self);
end;
function TModelMapsGMapsDrawMarker.DataSet: iModelMapsDataSet<iModelMapsDrawMarker>;
begin
Result := FDataSet;
end;
destructor TModelMapsGMapsDrawMarker.Destroy;
begin
inherited;
end;
function TModelMapsGMapsDrawMarker.&End: iModelMapsDraw;
begin
Result := FParent;
end;
function TModelMapsGMapsDrawMarker.InfoWindow: iModelMapsInfoWindow<iModelMapsDrawMarker>;
begin
FInfoWindow := TModelMapsInfoWindow<iModelMapsDrawMarker>.New(Self);
Result := FInfoWindow;
end;
class function TModelMapsGMapsDrawMarker.New(
Parent: iModelMapsDraw): iModelMapsDrawMarker;
begin
Result := Self.Create(Parent);
end;
function TModelMapsGMapsDrawMarker.ResultScript(MapName: string): String;
var
I: Integer;
Markers, GeoCode, LatValue, LngValue, InfoWindow, InfoContent: string;
begin
Markers := 'let markers' + MapName + ' = [];';
FDataSet.Dataset.First;
for I := 0 to Pred(FDataSet.DataSet.RecordCount) do
begin
LatValue := TUtilitiesStr.FloatCurrFieldToStrValue(FDataSet.DataSet.FieldByName(FDataSet.LatName));
LngValue := TUtilitiesStr.FloatCurrFieldToStrValue(FDataSet.DataSet.FieldByName(FDataSet.LngName));
InfoWindow := '';
if Assigned(FInfoWindow) then
begin
InfoContent := FDataSet.Dataset.FieldByName(FDataSet.LabelName).AsString;
if FDataSet.DataSet.Fields.FindField(FDataSet.InfoName) <> nil then
InfoContent := FDataSet.Dataset.FieldByName(FDataSet.InfoName).AsString;
InfoWindow := 'addMarkerListener' + MapName + '(' +
'markers' + MapName + '[markers' + MapName + '.length -1],' +
'new google.maps.InfoWindow({' +
'content: "' + InfoContent + '",' +
FInfoWindow.MaxWidth +
FInfoWindow.MinWidth +
'}),' +
FInfoWindow.StartOpened +
');';
end;
if (LatValue <> '') and (LngValue <> '') then
begin
Markers := Markers + 'markers' + MapName + '.push(' +
'new google.maps.Marker({' +
'position: {' +
'lat:' + LatValue + ',' +
'lng:' + LngValue + '},' +
'label: {' +
'text: "' + FDataSet.Dataset.FieldByName(FDataSet.LabelName).AsString + '",' +
'color: "#FFFFFF",},' +
'title:"' + FDataSet.Dataset.FieldByName(FDataSet.LabelName).AsString + '",' +
'map: ' + MapName + ',' +
'})' +
');' +
InfoWindow;
end
else
begin
if FDataSet.DataSet.Fields.FindField(FDataSet.AddressName) <> nil then
begin
GeoCode := GeoCode + 'geocoder' + MapName + '.geocode(' +
'{ address: "' + FDataSet.DataSet.FieldByName(FDataSet.AddressName).AsString + '" },' +
'function (results, status) {' +
'if (status == "OK") {' +
'markers' + MapName + '.push(' +
'new google.maps.Marker({' +
'position: results[0].geometry.location,' +
'label: {' +
'text: "' + FDataSet.Dataset.FieldByName(FDataSet.LabelName).AsString + '",' +
'color: "#FFFFFF",'+
'},' +
'title: "' + FDataSet.Dataset.FieldByName(FDataSet.LabelName).AsString + '",' +
'map: ' + MapName + ',' +
'})' +
');' +
InfoWindow +
'bounds' + MapName + '.extend(results[0].geometry.location);' + MapName + '.fitBounds(bounds' + MapName + ');' +
'let pos = geoCodeResult' + MapName + '.findIndex(i => i.address === "' + FDataSet.DataSet.FieldByName(FDataSet.AddressName).AsString + '");' +
'if (pos === -1) {' +
'geoCodeResult' + MapName + '.push({' +
'id_address: "' + FDataSet.DataSet.FieldByName(FDataSet.IdAddressName).AsString + '",' +
'address:"' + FDataSet.DataSet.FieldByName(FDataSet.AddressName).AsString + '",' +
'lat: results[0].geometry.location.lat(),' +
'lng: results[0].geometry.location.lng(),' +
'});' +
'}' +
'}' +
'}' +
');';
end;
end;
FDataSet.Dataset.Next;
end;
Result := Markers + GeoCode;
Result := Result + 'for (var i = 0; i < markers' + MapName + '.length; i++) {' +
'bounds' + MapName + '.extend(markers' + MapName + '[i].position);' +
'}';
end;
end.