Saving and reloading Polygons in FNCGoogleMaps

I want a user in a Delphi VCL application to be able to create a Polygon in FNCGoogleMaps, modify it and then save it to a database so that it can be reloaded at a later date. I can create, modify and save it as a string of JSONArray using the AEventData.CustomData on the OnPolyElementEditEnd event. Is there any way of loading this JSONArray straight into a FNCMapsPolygon or FNCMapsCoordinateRecArray or do i have to parse it before loading it into a FNCMapsCoordinateRecArray?

Any help would be appreciated. Tim

Hi,

Yes, it requires some additional class wrapping, but here is the code.

type
  TCoordinateList = class(TObjectList<TTMSFNCMapsCoordinateItem>, ITMSFNCBaseListIO, ITMSFNCBasePersistenceIO)
  private
    FOwnerInterface: IInterface;
  protected
    function GetItemClass: TClass;
    function CreateObject(const AClassName: string; const ABaseClass: TClass): TObject;

    { IInterface }
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  public
    function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
  end;
procedure TForm1.TMSFNCGoogleMaps1PolyElementEditEnd(Sender: TObject;
  AEventData: TTMSFNCMapsEventData);
var
  p: TTMSFNCGoogleMapsPolyline;
  a: TCoordinateList;
  I: Integer;
begin
  if Assigned(AEventData.PolyElement) then
  begin
    if (AEventData.PolyElement is TTMSFNCGoogleMapsPolyline) then
    begin
      p := (AEventData.PolyElement as TTMSFNCGoogleMapsPolyline);
      a := TCoordinateList.Create;
      a.JSON := AEventData.CustomData;

      TMSFNCGoogleMaps1.BeginUpdate;

      try
        p.Recreate := True;
        p.Coordinates.Clear;
        for I := 0 to a.Count - 1 do
          p.Coordinates.Add.Assign(a[I]);

        TTMSFNCUtils.Log(p.Coordinates.JSON);
      finally
        a.Free;
      end;

      TMSFNCGoogleMaps1.EndUpdate;
    end;
  end;
end;
{ TCoordinateList }


function TCoordinateList.CreateObject(const AClassName: string;
  const ABaseClass: TClass): TObject;
begin
  Result := TTMSFNCMapsCoordinateItem.Create(nil);
end;

function TCoordinateList.GetItemClass: TClass;
begin
  Result := TTMSFNCMapsCoordinateItem;
end;


function TCoordinateList._AddRef: Integer;
begin
  if FOwnerInterface <> nil then
    Result := FOwnerInterface._AddRef else
    Result := -1;
end;


function TCoordinateList._Release: Integer;
begin
  if FOwnerInterface <> nil then
    Result := FOwnerInterface._Release else
    Result := -1;
end;


function TCoordinateList.QueryInterface(const IID: TGUID;
  out Obj): HResult;
const
  E_NOINTERFACE = HResult($80004002);
begin
  if GetInterface(IID, Obj) then Result := 0 else Result := E_NOINTERFACE;
end;

Pieter

Thank you for that but I think there is something wrong. I do not believe that TCoordinateList has a JSON property as a.JSON is an error. As i could not get this to work i was not able to progress with testing but what i had hoped for was something like:

procedure TFormBoundary.TMSFNCGoogleMaps1PolyElementEditEnd(Sender: TObject;
AEventData: TTMSFNCMapsEventData);
var p: TTMSFNCGoogleMapsPolyline;
a: TCoordinateList;
I: Integer;
begin
if Assigned(AEventData.PolyElement) then
begin
try
tblData.edit;
tblData['PolygonBoundary'] := AEventData.CustomData;
tblData.Post;
finally

end;

end;
end;

procedure TFormBoundary.LoadBoundary;
var aData : String;
ca : TTMSFNCMapsCoordinateRecArray;
pg: TTMSFNCGoogleMapsPolygon;
begin
if tblData['PolygonBoundary'] <> null then
begin
aData := tblData['PolygonBoundary'];
ca.LoadJSON(aData); // To be replaced with routine
TMSFNCGoogleMaps1.BeginUpdate;
pg := TTMSFNCGoogleMapsPolygon(TMSFNCGoogleMaps1.AddPolygon(ca));
pg.Editable := true;
pg.StrokeColor := clRed;
pg.StrokeWidth := 2;
TMSFNCGoogleMaps1.EndUpdate;
end;

end;

My apologies for the above formatting. Something went wrong as i copied it.

JSON is a property that is available when FMX.TMSFNCTypes unit is added. You will also need to add FMX.TMSFNCPersistence unit.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.