I am adapted a function you describe here https://support.tmssoftware.com/t/geocoding-with-callback-not-processed-into-datamodule/14166/6
My function returns a TTMSFNCGeocodingItems variable.
Thus I created a variable and destroy it later following this way, is it correct ?
procedure TfMigration.Button27Click(Sender: TObject);
var
coor: TTMSFNCMapsCoordinate;
Infos: string;
Georefs: TTMSFNCGeocodingItems;
Succes : Boolean;
begin
TRY
TRY
fPrincipal.FNCGeocoding.service := gsHere;
fPrincipal.FNCGeocoding.APIKey := 'xxxxxx';
Georefs := TTMSFNCGeocodingItems.Create(NIL);
Georefs := FPrincipal.GetGeocoding('london', Succes, Infos);
IF Succes THEN
Edit1.Text := Georefs.Items[0].Address;
EXCEPT
ON E: System.SysUtils.Exception DO
BEGIN
END;
END; // Fin de EXCEPT
FINALLY
Georefs.Free;
END; // Fin de FINALLY
end;
You need to destroy the variable inside the callback or event, because the request is asynchronous:
Latitude := '';
Longitude:= '';
geocoder := TTMSFNCGeocoding.Create(ServerContainer); //<== ServerContainer = DataModule
geocoder.Service := gsGoogle;
geocoder.APIKey := 'zzzzzzz';
// Check if CodePostal is ... a CodePOstal
// Retrieve Lat, Long from ZIP code
sTempAdr := CodePostal+',France';
geocoder.GetGeocoding(sTempAdr,
procedure(const ARequest: TTMSFNCGeocodingRequest; const ARequestResult: TTMSFNCCloudBaseRequestResult)
begin
Longitude := ARequest.Items[0].Coordinate.longitude.ToString;
Latitude := ARequest.Items[0].Coordinate.Latitude.ToString;
FreeAndNil(geocoder);
end
);
..
Well, this is a solution, but not the ideal one, we'll probably add a synchronous function that does the same thing instead, so before we update the documentation we'll need more time to investigate a permanent & more elegant solution to retrieve the coordinates in a synchronous way.